Hi guys!
I am currently making a jumping game where there is an object at the left side of the screen, on a platform. Once the object successfully jumps and lands on the platform on the right, it does the following:
-
the platform on the right moves to the left
-
the platform on the left (that you JUST jumped off from) moves off-screen.
-
a new platform is supposed to appear at the right side of the screen, thus continuing the loop.
I have already made the functions where it allows the object to jump and show if the collision is successful or not. My problem is, the 3 things I have mentioned above happens, but it keeps on going and does not stop for the object to make the next jump. This makes the object go off screen as well, since the platforms keep moving towards the left. After debugging, I feel like the problem lies where the collision happens. This is because, once it touches the platform, the collision keeps on happening until the object is off of the platform. I was wondering if you guys can help me with this!
Here is part of my code that is relevant:
local onPlatform = false local gameStarted = false function playerCollision( self, event ) if ( event.phase == "began" ) then --if hit bottom column, u get points if event.target.type == "player" and event.other.type == "bottomColumn" then print ("hit column") onPlatform = true else --if hit anything else, gameOver --composer.gotoScene( "restart" ) print ("hit ground") end end end function moveColumns() for a = elements.numChildren,1,-1 do --speed of columns moving --if greater than -100, keep moving it to left --puts platform at the right and stops if (elements[a].x \> display.contentWidth/1.1) then elements[a].x = elements[a].x - 12 end --moves platform to left after it successfully lands if (onPlatform == true) then if (elements[a].x \> display.contentWidth/3 and elements[a].x \< display.contentWidth/1.11) then elements[a].x = elements[a].x - 12 end end --moves left platform to off-screen and deletes after it passes a certain X-axis if (onPlatform == true) then if(elements[a].x \> -100 and elements[a].x \< display.contentWidth/2.99) then elements[a].x = elements[a].x - 12 --adds score if it goes past a certain X-axis at left side elseif(elements[a].x \< -100) then mydata.score = mydata.score + 1 tb.text = mydata.score elements[a].scoreAdded = true elements:remove(elements[a]) elements[a] = nil end end end end