#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Stick direction?

in General Posts: 50

I am trying to make the stick figure go in the direction that is the difference from where I fist touched and where my finger is moving now
This is what I have, any help would be appreciated

``````-- Stick

-- Use this function to perform your initial setup
function setup()
angH = 0
time = 0
motion = "standing"
stickx = 100
sticky = 100
speed = 0
end
function touched(touch)
if touch.state == BEGAN then
startx = touch.x
starty = touch.y
elseif touch.state == MOVING then
angH=angH-(touch.deltaX)%360
speed=(touch.y-starty)/250
if speed > 1 then
speed = 1
elseif speed < -1 then
speed = -1
end
elseif touch.state == ENDED then
speed= 0
end
end
-- This function gets called once every frame
function draw()

background(255, 255, 255, 255)
time = time + 1
-- This sets the line thickness
strokeWidth(5)
x=speed*math.sin(math.rad(angH))
y=speed*math.cos(math.rad(angH))
if time > 30 then
if motion == "standing" then
motion = "walking"
else
motion = "standing"
end
time=0
end
stickx = stickx + x
sticky = sticky + y
if motion == "standing" then
sprite("Documents:stand",stickx,sticky,40,100)
else
sprite("Documents:walk",stickx,sticky,40,100)
end

-- Do your drawing here

end

``````

## Comments

• Posts: 9,260

If all you want is the stick figure to go towards your finger, this will work. Just a suggestion. If you use sprites other than those that come with Codea, other users can’t see or use them. We can’t see your Documents walk or stand sprites. For me, I just see a black rectangle. If you’re going to post code with sprites, replace your sprites with some close Codea sprites just so we see something.

``````function setup()
time = 0
motion = "standing"
stickx = 100
sticky = 100
mx,my=stickx,sticky
end

function touched(t)
if t.state==MOVING then
mx,my=t.x,t.y
end
end

function draw()
background(255, 255, 255, 255)
time = time + 1
if time > 30 then
if motion == "standing" then
motion = "walking"
else
motion = "standing"
end
time=0
end
stickx = stickx - (stickx-mx)/25
sticky = sticky - (sticky-my)/25
if motion == "standing" then
sprite("Documents:stand",stickx,sticky,40,100)
else
sprite("Documents:walk",stickx,sticky,40,100)
end
end
``````
• Posts: 50

How would you make it with a joystick?

• edited December 2017 Posts: 9,260

Here’s something I had.

``````displayMode(FULLSCREEN)

function setup()
inner=80    -- size of inner circle
outer=200   -- size of outer circle
diff=outer/2-inner/2
sx,sy=WIDTH/2,HEIGHT/2
end

function draw()
background(40, 40, 50)
sprite("Planet Cute:Character Horn Girl",sx,sy)
js()
end

function js()
if show then
v1=vec2(cx,cy)
d=v1:dist(vec2(x,y))
if d<diff then
px,py=x,y
else
a=diff/d
px=(x-cx)*a+cx
py=(y-cy)*a+cy
end
stroke(255)
strokeWidth(4)
noFill()
ellipse(cx,cy,outer)
fill(255)
ellipse(px,py,inner)
v1=vec2(px,py)
d1=v1:dist(vec2(cx,cy))
sp=d1/4+1
sx=sx+(px-cx)/sp
sy=sy+(py-cy)/sp
end
end

function touched(t)
if t.state==BEGAN then
cx,cy=t.x,t.y
x,y=cx,cy
show=true
end
if t.state==MOVING then
x,y=t.x,t.y
end
if t.state==ENDED then
show=false
end
end
``````
• Posts: 50

This is exactly what I wanted thanks

Sign In or Register to comment.