Hello. I’m pretty new to Codea, and I got some questions. Im trying to recreate a game called “Gravitron”. So I need some help:
Is there a way to perform a perpetual move of and object, for example, an ellipse, that makes the Y of the ellipse go up and down for always when code runs.
Is there a way to make the Controls of moving left and right (in my code) permanent when they are being touched? For example (in my code), when the controls are touched, it only moves the value, and then, you need to touch again. Is there a way to when they are being touched, they move and move before the stop being touched.
Thanks you so much for your help
EDIT: This is my code
-- Test Gravitron Game
p = vec2(WIDTH/2)
-- Use this function to perform your initial setup
function setup()
displayMode( FULLSCREEN )
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(0, 0, 0, 255)
-- This sets the line thickness
strokeWidth(5)
-- Do your drawing here
fill(177, 133, 46, 255)
noStroke()
ellipse(p.x, HEIGHT/2, 150)
sprite("Cargo Bot:Command Right", WIDTH - 50 , 50 , 70, 70)
sprite("Cargo Bot:Command Left", 50, 50, 70, 70)
if p.x < 1 then
p.x = WIDTH - 2
end
if p.x > WIDTH - 1 then
p.x = 2
end
end
function touched(t)
if CurrentTouch.x > WIDTH - 100 and CurrentTouch.y < 100 then
p.x = p.x + 10
end
if CurrentTouch.x < 100 and CurrentTouch.y < 100 then
p.x = p.x - 10
end
end
CurrentTouch holds the most recent state, however long ago it happened (so it can mislead your program into thinking the touch continues), whereas the touched function is only triggered by current touches.
When using touched, you should check the state property, which can be BEGAN, MOVING or ENDED. If you’re just trapping a touch on the screen, then either BEGAN or ENDED will do.
Touches can be quite tricky, though, and take a little getting used to.
You can use CurrentTouch - but it only remembers the LAST touch event that happened, if you touch the screen in multiple places at the same time you’ll probably miss the inbetween states.
Using the touched function ensures that none of these are missed as you can track each touch using the t.id field.
@CayDay47 I think that CurrentTouch in the touched function might be one frame behind, but besides, why use a single touch (which can cause errors if you have multiple fingers on the screen) when you have multitouch available easily?