# Help (Touch)

I need help on how to control touches Everytime i touch the screen I want my ball to go up like in the game flappy bird or my new favourite dont touch the spikes. Any help helps No code to share. I’m starting it on paper so I can understand better. Also, when is Codea Play coming out. Yeah thats all Ill stop rambling

Welcome to the Codea forums! There’s not really too much to help if there’s no code, but I guess I could explain a couple of ways to do it.

If you’re using the built-in physics, you can just call `physicsBody:applyForce(vec2(0, 100))`. It’ll send the object flying upwards.

If you’re using custom physics (x and y variables, I recommend you have motionx and motiony too), you could either add to the y, or maybe add to the motiony if you have it.

I’ve built all the outside boundaries (top bottom left right) with physics.body then I drew a ball with gravity etc to make it drop. Does that help explain

Is the ball a physics body too? Then you can just do what SkyTheCoder said.

@GriffinC Here an example. Tap the middle of the screen to go up. Tap the right or left side to go left or right.

``````
displayMode(FULLSCREEN)

function setup()
e1=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
e2=physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))
e3=physics.body(EDGE,vec2(WIDTH,HEIGHT),vec2(WIDTH,0))
e4=physics.body(EDGE,vec2(WIDTH,0),vec2(0,0))
b1=physics.body(CIRCLE,20)
b1.x=WIDTH/2
b1.y=100
end

function draw()
background(40, 40, 50)
fill(255)
ellipse(b1.x,b1.y,40)
end

function touched(t)
if t.state==BEGAN then
if t.x<WIDTH*.25 then
b1:applyForce(vec2(-100,0))   -- left
elseif t.x>WIDTH*.75 then
b1:applyForce(vec2(100,0))    -- right
else
b1:applyForce(vec2(0,500))  -- up
end
end
end

``````

Thanks Dave that is perfect!

We all start out like that, for me I just realised one day (after dave posted one of his examples) that it’s exactly like CurrentTouch except the data isn’t transferred in to a variable its from the touch call sent to Codea which happens after the draw call. So you have a ball with currentTouch, that’s easy enough.

``````function draw()
background(40,40,50)
fill(255)
ellipse(CurrentTouch.x,CurrentTouch.y,vec2(CurrentTouch.deltaX,CurrentTouch.deltaY):len()+10)
end
``````

That gives you a ball that will follow your touch and expand and contract based of the delta of your touch position (the speed in other words).

This is the same code but using the touched function:

``````function setup()
ep = vec2(WIDTH/2,HEIGHT/2)
ed = vec2()
end

function touched(t)
ep = vec2(t.x,t.y)
ed = vec2(t.deltaX,t.deltaY)
end

function draw()
background(40,40,50)
fill(255)
ellipse(ep.x,ep.y,ed:len()+10)
end
``````

Because the touched function returns the current state of the touch, we can set a random colour for the ellipse every time we touch the screen by doing this:

``````function setup()
ep = vec2(WIDTH/2,HEIGHT/2)
ed = vec2()
ec = color(255,255,255)
end

function touched(t)
ep = vec2(t.x,t.y)
ed = vec2(t.deltaX,t.deltaY)
if t.state == BEGAN then
ec = color(math.random(0,255),math.random(0,255),math.random(0,255))
end
end

function draw()
background(40,40,50)
fill(ec)
ellipse(ep.x,ep.y,ed:len()+10)
end
``````

These are the basic examples, once your comfortable with playing around with these then take a look at the multi touch example in Codea’s example list.

@aurumcoder2624 in what sense do you have no idea? There is an example that demonstrates multiple touches in the examples.

Like I’m okay with CurrentTouch, but touch? Forget it.