# Is this going to need a stationary stick?

So after help from the forum and reading some documents, I am slowly grasping Lua. At the moment I have the joystick that generates wherever the current touch is. This is good if your into a game and don’t press exactly on the same spot every time to move (some of us turn our iPads when making a sharp turn, ha) so I am working on a fire button and have gotten the ellipse drawn. I just threw up default coordinates for now, I’ll adjust x,y,w,h later, for now I’m concerned with multiple touches. If I touch the button, the game thinks I’m just adjusting the joystick position. I am using current.touch, but is there a multiple touch command that can distinguish movement from the push button? Even before I had the button drawn, if I was moving, and then tapped where a button would be, the character would jump across the screen since it thought I was suddenly sliding all the way in that direction.

``````
function setup()
x,y=0,0
sx=WIDTH/2
sy=HEIGHT/2
end

function draw()
background(0, 0, 255, 255)
fill(255)

w , h = spriteSize("Planet Cute:Character Boy")

if sx > WIDTH -w/4 then
sx = WIDTH -w/4
end
if sy > HEIGHT -h/4 then
sy = HEIGHT -h/4
end
if sx < w/4 then
sx = w/4
end
if sy < h/4 then
sy = h/4
end

sprite("Planet Cute:Character Boy",sx,sy)
if x+y>0 then
sx=sx+(tx-x)/10
sy=sy+(ty-y)/10
fill(255)
ellipse(x,y,10)    -- draw circle center
noFill()
stroke(255)
strokeWidth(4)
ellipse(x,y,100)    -- draw outer circle
end

ellipse(400, 400, 100, 100)
fill(255, 0, 0, 255)
end
function touched(touch)

if CurrentTouch.x > 400 and CurrentTouch.x < 500 and CurrentTouch.y > 400 and CurrentTouch.y < 500 then
print("confirmed")
else
return
end

if CurrentTouch.state == ENDED then
return
end

end

function touched(t)
if t.state==BEGAN then    -- starting center point
x=t.x
y=t.y
tx=x
ty=y
elseif t.state==MOVING then
tx=t.x
ty=t.y
elseif  t.state==ENDED then    -- done moving
x,y=0,0

end

end

``````

Wow that’s some ugly code…

If you need any extra information, please let me know. Thank you.

see row multi-touch example project in Codea (scroll along the top row of projects and you’ll find it)

the basic idea is that you have a table of touches which you identify with each touches unique identifier (touch. id) and you update in the touched function.

For each touch:

Test to see if it is on the button-

If it is then fire

Else move with the joystick

That’s kind of what JakAttak as saying

Ahh that makes sense, thank you, I saw an example with touch.id but didn’t quite understand it, I’ll dive deeper into it. Thanks again.

Ok so I used some of the code for multitouch, I took out that it draws an ellipse since there is already one permanently in place, but I am unsure of how to describe each touch.id. Is [touch.id] in the code a variable? With the code I have now, with it = nil it just errors out and won’t even move the sprite now. So if I’m thinking right, I want to verify multi touches, and then from there I can designate what happens when the button is pressed:

``````
function setup()
x,y=0,0
sx=WIDTH/2
sy=HEIGHT/2
end

function draw()
background(0, 0, 0, 255)
fill(255)

w , h = spriteSize("Planet Cute:Character Boy")

if sx > WIDTH -w/4 then
sx = WIDTH -w/4
end
if sy > HEIGHT -h/4 then
sy = HEIGHT -h/4
end
if sx < w/4 then
sx = w/4
end
if sy < h/4 then
sy = h/4
end

sprite("Planet Cute:Character Boy",sx,sy)
if x+y>0 then
sx=sx+(tx-x)/10
sy=sy+(ty-y)/10
fill(255)
ellipse(x,y,10)    -- draw circle center
noFill()
stroke(255)
strokeWidth(4)
ellipse(x,y,100)    -- draw outer circle
end

ellipse(650, 120, 100, 100)
fill(255, 0, 0, 255)
end
function touched(touch)

-- keep track of our touches in this table
touches = {}
end

-- This function gets called whenever a touch
--  begins or changes state
function touched(touch)
if touch.state == ENDED then
touches[touch.id] = nil
else
touches[touch.id] = touch
end
end

if CurrentTouch.x > 400 and CurrentTouch.x < 500 and CurrentTouch.y > 400 and CurrentTouch.y < 500 then
print("confirmed")
else
return
end

if CurrentTouch.state == ENDED then
return
end

function touched(t)
if t.state==BEGAN then    -- starting center point
x=t.x
y=t.y
tx=x
ty=y
elseif t.state==MOVING then
tx=t.x
ty=t.y
elseif  t.state==ENDED then    -- done moving
x,y=0,0

end

end

``````

If I am going in the wrong order, please let me know. Thank you.

@Theonegoku Try this.

``````
displayMode(FULLSCREEN)

function setup()
cx=WIDTH/2
cy=HEIGHT/2
x,y=0,0
sp=1    -- speed
mid=0
end

function draw()
background(40,40,50)
stroke(255)
strokeWidth(2)
if x+y>0 then
cx=cx+(tx-x)/sp
cy=cy+(ty-y)/sp
line(x,y,lx,ly)
end
sprite("Planet Cute:Character Boy",cx,cy)
fill(255)
ellipse(WIDTH-100,100,100)
fill(255,0,0)
text("FIRE",WIDTH-100,100)
if fire then
text("fire",cx,cy-75)
end
end

function touched(t)
if t.state==BEGAN then
fire=false
if t.x>WIDTH-150 and t.y<150 then
fire=true
fid=t.id
elseif mid==0 then
mid=t.id
x=t.x
y=t.y
tx=x
ty=y
lx=x
ly=y
end
end
if t.state==MOVING and t.id==mid then
lx=t.x
ly=t.y
v1=vec2(x,y)
d1=v1:dist(vec2(t.x,t.y))
tx=t.x
ty=t.y
sp=d1/5+1
end
if t.state==ENDED then
if t.id==mid then    -- done moving
x,y=0,0
mid=0
elseif t.id==fid then    -- done fireing
fire=false
end
end
end

``````

@dave1707 that’s great man, thank you. I will stop progress for the day and dissect this to understand the code more. I also like the way you formatted it to be easily read. If I want bullets in the future I can just change “if fire then text” to the more complicated stuff with firing bullets with velocity thank you again.

Second note, in case someone sees this and uses it as a template. Not sure if it was intentional or not, but Dave’s version eliminated the screen borders, this turned out as a blessing as his variables were a little different than the original so updating the screen to fit his version and succeeding felt good and let me know that I was learning, I won’t post the code here, as it will leave the challenge for future users. Thanks again.

@Theonegoku Actually the version I gave you was one that I already had. I just added the fire button and the id’s to keep the 2 touches seperated. I didn’t notice the code in your version to keep the sprite on the screen, so it wasn’t done intentionally. Glad to read that you were able to modify the code to add what you needed. Like you said, that shows that you are learning and that feeling will continue the more you write and modify code.