Hello all!
I have gotten a little further in my first test game. The game is simply a background with ellipses raining down and when you tap on an ellipse it destroys the ellipse and increases the score. I have the score working and the ellipse is getting destroyed when I tap it, however Codea seems to be saving where I touched and destroys any ellipse that hits the last spot that I touched. I want the ellipse to be destroyed only when I first tap. I can’t seem to figure it out, I assume I need to use touch.began instead of CurrentTouch, but don’t know how to use x and y positions with touch.began…
Any insight would be greatly appreciated, here is the code:
-- Testtttt
supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)
function setup()
tab={} -- table for asteroids
AsteroidWidth=100 -- asteroid width
AsteroidHeight=100 -- asteroid height
EllipseStart=818 -- asteroid starting y position
EllipseSpeed=-15 -- asteroid falling speed
timer=0
asteroidFrequency=1 -- asteroid spawn frequency
score=0 -- starting score
end
function LaunchEllipse()
-- insert asteroid x,y values in table
table.insert(tab,vec2(math.random(50,WIDTH-50),EllipseStart))
end
function draw()
-- draw background
sprite("Cargo Bot:Starry Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
-- draw score
fill(0, 0, 0, 255)
text("Score: " .. score, WIDTH-100, HEIGHT-100)
-- draw asteroids
fill(255, 255, 0, 255)
for a,b in pairs(tab) do
ellipse(b.x,b.y,AsteroidWidth,AsteroidHeight)
b.y=b.y+EllipseSpeed
end
-- remove asteroids that go off bottom of screen
for a,b in pairs(tab) do
if b.y<0 then
table.remove(tab,a)
end
end
-- check timer to launch next asteroid
timer=timer+DeltaTime
if timer>asteroidFrequency then
LaunchEllipse()
timer=0
end
-- destroy asteroid when tapped and increase score by 100
for a,b in pairs(tab) do
if CurrentTouch.x>b.x-50 and CurrentTouch.x<b.x+50
and CurrentTouch.y>b.y-50 and CurrentTouch.y<b.y+50 then
table.remove(tab,a)
score=score+100
end
end
end
Thanks in advance!
You don’t want “moving touch destroy” ,right?
Not sure what that means. I just want the ellipse to be destroyed when I first tap it, after that I don’t want it destroyed. So if I am holding my finger on the screen and the ellipse passes the spot I am touching I don’t want it destroyed, if that makes sense.
Think I need to be using the touch.BEGAN function, but not sure how to use it with x and y coordinates.
I tried to do
-- destroy asteroid when tapped and increase score by 100
for a,b in pairs(tab) do
if CurrentTouch == BEGAN then
if CurrentTouch.x>b.x-50 and CurrentTouch.x<b.x+50
and CurrentTouch.y>b.y-50 and CurrentTouch.y<b.y+50 then
table.remove(tab,a)
score=score+100
end
end
Which I thought would work, but now the ellipses don’t get destroyed.
change the line that says if CurrentTouch == BEGAN
to say if Current Touch.state == BEGAN
i would also recommend using the touched function as opposed to CurrentTouch.
Nice! That seems to work for the most part, for some reason I have to tap under the ellipse to destroy it, I thought my coordinates for tapping to destroy are correct but they must be off. I’ll have to try to figure that out.
Thanks for the help!
The new code:
-- Testtttt
supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)
function setup()
tab={} -- table for asteroids
AsteroidWidth=100 -- asteroid width
AsteroidHeight=100 -- asteroid height
EllipseStart=818 -- asteroid starting y position
EllipseSpeed=-15 -- asteroid falling speed
timer=0
asteroidFrequency=1 -- asteroid spawn frequency
score=0 -- starting score
end
function LaunchEllipse()
-- insert asteroid x,y values in table
table.insert(tab,vec2(math.random(50,WIDTH-50),EllipseStart))
end
function draw()
-- draw background
sprite("Cargo Bot:Starry Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
-- draw score
fill(0, 0, 0, 255)
text("Score: " .. score, WIDTH-100, HEIGHT-100)
-- draw asteroids
fill(255, 255, 0, 255)
for a,b in pairs(tab) do
ellipse(b.x,b.y,AsteroidWidth,AsteroidHeight)
b.y=b.y+EllipseSpeed
end
-- remove asteroids that go off bottom of screen
for a,b in pairs(tab) do
if b.y<0 then
table.remove(tab,a)
end
end
-- check timer to launch next asteroid
timer=timer+DeltaTime
if timer>asteroidFrequency then
LaunchEllipse()
timer=0
end
-- destroy asteroid when tapped and increase score by 100
for a,b in pairs(tab) do
if CurrentTouch.state == BEGAN then
if CurrentTouch.x>b.x-50 and CurrentTouch.x<b.x+50
and CurrentTouch.y>b.y-50 and CurrentTouch.y<b.y+50 then
table.remove(tab,a)
score=score+100
end
end
end
end
Maybe the touch coordinates not matching the ellipse coordinates has to do with ellipseMode?
We’ll I finally got the touch to destroy working. I was having trouble getting the touch coordinates and the asteroid coordinates to match. I got it to work but the Y coordinates of the touch is like 100 pixels above what it logically should be… I don’t understand why that happened…
-- Testtttt
supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)
function setup()
tab={} -- table for asteroids
AsteroidWidth=100 -- asteroid width
AsteroidHeight=100 -- asteroid height
EllipseStart=818 -- asteroid starting y position
EllipseSpeed=-15 -- asteroid falling speed
timer=0
asteroidFrequency=1 -- asteroid spawn frequency
score=0 -- starting score
rectMode(CENTER)
end
function LaunchEllipse()
-- insert asteroid x,y values in table
table.insert(tab,vec2(math.random(50,WIDTH-50),EllipseStart))
end
function draw()
-- draw background
sprite("Cargo Bot:Starry Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
-- draw score
fill(0, 0, 0, 255)
text("Score: " .. score, WIDTH-100, HEIGHT-100)
-- draw asteroids
fill(255, 255, 0, 255)
for a,b in pairs(tab) do
rect(b.x,b.y,AsteroidWidth,AsteroidHeight)
b.y=b.y+EllipseSpeed
end
-- remove asteroids that go off bottom of screen
for a,b in pairs(tab) do
if b.y<-50 then
table.remove(tab,a)
end
end
-- check timer to launch next asteroid
timer=timer+DeltaTime
if timer>asteroidFrequency then
LaunchEllipse()
timer=0
end
-- destroy asteroid when tapped and increase score by 100
for a,b in pairs(tab) do
if CurrentTouch.state == BEGAN then
if CurrentTouch.x>b.x-50 and CurrentTouch.x<b.x+50
and CurrentTouch.y>b.y+50 and CurrentTouch.y<b.y+150 then
table.remove(tab,a)
score=score+100
end
end
end
end
@Crumble The problem with your touch is timing. You draw the rectangle, then you add the EllipseSpeed. The means that b.y is now 15 pixels below where you drew the rectangle. Then you check the touch position, so the rectangle image and it’s center position isn’t the same when you check touch.