When i first start the game it plays fine, going from the menu to playing it and you can move the sprite up and down the screen fine. Then when you crash and go to restart the game the sprite drops down extremely fast but when you touch to send move it up it goes up extremely slow and if you let up you can’t recover from it falling. Any ideas of what I could be doing wrong?
How are you moving it down in the first place? Physics body with gravity? Transition? enterFrameEvent? something else?
With a physics body on it, and using applyForce()
Hi @jamesbaldwin94,
You may need to provide more information for other developers to assist you. If I had to guess, it’s because you’re applying force twice (or more) to the object without stopping it. Remember that “applyForce()” is a cumulative physics operation, so if you apply it multiple times, the object will just keep going faster and faster. Force can also be a very sensitive operation… for example, depending on the object’s mass/density, even a small amount of force may send it speeding wildly in the applied direction.
On reset, you may want to try resetting the body’s velocity to (0,0) via:
[lua]
object:setLinearVelocity( 0, 0 )
[/lua]
Hope this helps,
Brent
local storyboard = require( "storyboard" ) local scene = storyboard.newScene() -- ================================= -- -- required library files below here -- --------------------------------------- local physics = require("physics"); physics.start(false); physics.setGravity( 0, 9.8 ) --------------------------------------- -- required library files above here -- -- ================================= -- -- ================================ -- -- local vars/references below here -- -------------------------------------- local gameBg; local cloud; local cloud2; local bird; local crashedBird; local topBar; local bottomBar; local topOfScreen; local bottomOfScreen; local group; \_W = display.viewableContentWidth \_H = display.viewableContentHeight -------------------------------------- -- local vars/references above here -- -- ================================ -- -- =================================== -- -- functions for this scene below here ----------------------------------------- ----------------------------------------- -- functions for this scene above here -- =================================== -- ------------------ -- -- birdReady -- ------------------ function birdReady() bird.bodyType = "dynamic"; end ------------------ -- -- end birdReady -- ------------------ ------------------ -- -- activateBird -- ------------------ function activateBird(self,event) self:applyForce(0, -0.5, self.x, self.y); end ------------------ -- -- End activateBird -- ------------------ ------------------ -- -- Touch Screen Function -- ------------------ function touchScreen(event) if(event.phase == "began") then bird.enterFrame = activateBird; Runtime:addEventListener("enterFrame",bird); end if(event.phase == "ended") then Runtime:removeEventListener("enterFrame", bird); end return true; end ----------------- -- -- TouchScreen End -- ----------------- ----------------- -- -- gameOver() -- ----------------- function gameOver() bird.collided = false; storyboard.gotoScene("menu", { effect = defaultTransition }); --physics.setGravity( 0, 9.8 ) end ----------------- -- -- end gameOver() -- ----------------- ----------------- -- -- crashed() -- ----------------- function crashed() crashedBird.x = bird.x; crashedBird.y = bird.y; crashedBird.isVisible = true; bird.bodyType = "static"; bird.isVisible = false; gameOver(); end ----------------- -- -- end crashed() -- ----------------- ----------------- -- -- onCollision() -- ----------------- function onCollision(event) if(event.phase == "began") then if(bird.collided == false) then bird.collided = true; crashed(); end end end ----------------- -- -- end onCollision() -- ----------------- ----------------- -- --Event Listener for touch -- ----------------- Runtime:addEventListener("touch", touchScreen); ----------------- -- -- End event Listener for touch -- ----------------- -- Called when the scene's view does not exist: function scene:createScene( event ) group = self.view -- CREATE display objects and add them to 'group' here. ------------------- -- -- Bird -- ------------------- bird = display.newImage("images/bird.png"); bird.x = (\_W / 2) - 90; bird.y = \_H / 2; bird.xScale = 0.7; bird.yScale = 0.8; bird.collided = false; physics.addBody(bird,"static", {density = 0.1, friction = 0.5, bounce = 0.1, radius = 12}); group:insert(bird); birdIntro = transition.to(bird, {time = 2000, x = 100, onComplete = birdReady}); -------------------- -- -- End Bird -- -------------------- -------------------- -- -- Crashed Bird -- -------------------- crashedBird = display.newImage("images/crashed.png"); crashedBird.x = 100; crashedBird.y = 100; crashedBird.isVisible = false; group:insert(crashedBird); -------------------- -- -- End Crashed Bird -- -------------------- end -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) local group = self.view globalGroup = display.newGroup(); group:insert(globalGroup); -- INSERT code here (e.g. start timers, load audio, start listeners, etc.) --storyboard.purgeScene("menu"); --Purge the end game file if added storyboard.loadScene("menu"); --Runtime listener for the touch on the screen Runtime:addEventListener("touch", touchScreen); --Moves the cloud from right to left cloud.enterFrame = scrollCloud; Runtime:addEventListener("enterFrame", cloud); --Moves the cloud from right to left cloud2.enterFrame = scrollCloud; Runtime:addEventListener("enterFrame", cloud2); --Moves the top barricade from right to left topBar.enterFrame = scrollCloud; Runtime:addEventListener("enterFrame", topBar); --Moves the bottom barricade from right to left bottomBar.enterFrame = scrollCloud; Runtime:addEventListener("enterFrame", bottomBar); --Detects the collision Runtime:addEventListener("collision", onCollision); end -- Called when scene is about to move offscreen: function scene:exitScene( event ) local group = self.view bird:removeSelf(); gameBg:removeSelf(); cloud:removeSelf(); cloud2:removeSelf(); crashedBird:removeSelf(); topBar:removeSelf(); bottomBar:removeSelf(); topOfScreen:removeSelf(); bottomOfScreen:removeSelf(); group:removeSelf(); -- INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.) Runtime:removeEventListener("touch", touchScreen); Runtime:removeEventListener("enterFrame", bird); Runtime:removeEventListener("enterFrame", cloud); Runtime:removeEventListener("enterFrame", cloud2); Runtime:removeEventListener("enterFrame", topBar); Runtime:removeEventListener("enterFrame", bottomBar); Runtime:removeEventListener("collision", onCollision); --storyboard.removeAll ( ) end -- Called prior to the removal of scene's "view" (display group) function scene:destroyScene( event ) local group = self.view --storyboard.removeAll(); -- INSERT code here (e.g. remove listeners, widgets, save state, etc.) --bird:removeSelf(); -- INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.) Runtime:removeEventListener("touch", touchScreen); Runtime:removeEventListener("enterFrame", bird); Runtime:removeEventListener("enterFrame", cloud); Runtime:removeEventListener("enterFrame", cloud2); Runtime:removeEventListener("enterFrame", topBar); Runtime:removeEventListener("enterFrame", bottomBar); Runtime:removeEventListener("collision", onCollision); group = nil; end --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- "createScene" event is dispatched if scene's view does not exist scene:addEventListener( "createScene", scene ) -- "enterScene" event is dispatched whenever scene transition has finished scene:addEventListener( "enterScene", scene ) -- "exitScene" event is dispatched before next scene's transition begins scene:addEventListener( "exitScene", scene ) -- "destroyScene" event is dispatched before view is unloaded, which can be -- automatically unloaded in low memory situations, or explicitly via a call to -- storyboard.purgeScene() or storyboard.removeScene(). scene:addEventListener( "destroyScene", scene ) --------------------------------------------------------------------------------- return scene
@BrentSorrentino Here’s most of the code that would control the sprite’s movement. I tried setting the Velocity back to 0,0 but it had no effect on the sprite in the second play. Thanks for any help!
How do you reset the scene? I don’t see it here. When you go back to menu scene try removing the game scene so it destroys it and then recreates it. You are adding event listeners twice here. Once in create scene and once in enter scene. They should only be in inter scene. In createScene create objects and other thing in enterScene start things. In exitScene stop things in destroyScene clean up if necessary.
Thank you! You pointing out that I had added an eventlistener twice allowed me to figure it out! It was that my applyForce was so low compared to gravity so it had no real impact on the movement. I can’t believe I missed that! But thank you, I’ve been stuck on that for the past 4 days.
How are you moving it down in the first place? Physics body with gravity? Transition? enterFrameEvent? something else?
With a physics body on it, and using applyForce()
Hi @jamesbaldwin94,
You may need to provide more information for other developers to assist you. If I had to guess, it’s because you’re applying force twice (or more) to the object without stopping it. Remember that “applyForce()” is a cumulative physics operation, so if you apply it multiple times, the object will just keep going faster and faster. Force can also be a very sensitive operation… for example, depending on the object’s mass/density, even a small amount of force may send it speeding wildly in the applied direction.
On reset, you may want to try resetting the body’s velocity to (0,0) via:
[lua]
object:setLinearVelocity( 0, 0 )
[/lua]
Hope this helps,
Brent
local storyboard = require( "storyboard" ) local scene = storyboard.newScene() -- ================================= -- -- required library files below here -- --------------------------------------- local physics = require("physics"); physics.start(false); physics.setGravity( 0, 9.8 ) --------------------------------------- -- required library files above here -- -- ================================= -- -- ================================ -- -- local vars/references below here -- -------------------------------------- local gameBg; local cloud; local cloud2; local bird; local crashedBird; local topBar; local bottomBar; local topOfScreen; local bottomOfScreen; local group; \_W = display.viewableContentWidth \_H = display.viewableContentHeight -------------------------------------- -- local vars/references above here -- -- ================================ -- -- =================================== -- -- functions for this scene below here ----------------------------------------- ----------------------------------------- -- functions for this scene above here -- =================================== -- ------------------ -- -- birdReady -- ------------------ function birdReady() bird.bodyType = "dynamic"; end ------------------ -- -- end birdReady -- ------------------ ------------------ -- -- activateBird -- ------------------ function activateBird(self,event) self:applyForce(0, -0.5, self.x, self.y); end ------------------ -- -- End activateBird -- ------------------ ------------------ -- -- Touch Screen Function -- ------------------ function touchScreen(event) if(event.phase == "began") then bird.enterFrame = activateBird; Runtime:addEventListener("enterFrame",bird); end if(event.phase == "ended") then Runtime:removeEventListener("enterFrame", bird); end return true; end ----------------- -- -- TouchScreen End -- ----------------- ----------------- -- -- gameOver() -- ----------------- function gameOver() bird.collided = false; storyboard.gotoScene("menu", { effect = defaultTransition }); --physics.setGravity( 0, 9.8 ) end ----------------- -- -- end gameOver() -- ----------------- ----------------- -- -- crashed() -- ----------------- function crashed() crashedBird.x = bird.x; crashedBird.y = bird.y; crashedBird.isVisible = true; bird.bodyType = "static"; bird.isVisible = false; gameOver(); end ----------------- -- -- end crashed() -- ----------------- ----------------- -- -- onCollision() -- ----------------- function onCollision(event) if(event.phase == "began") then if(bird.collided == false) then bird.collided = true; crashed(); end end end ----------------- -- -- end onCollision() -- ----------------- ----------------- -- --Event Listener for touch -- ----------------- Runtime:addEventListener("touch", touchScreen); ----------------- -- -- End event Listener for touch -- ----------------- -- Called when the scene's view does not exist: function scene:createScene( event ) group = self.view -- CREATE display objects and add them to 'group' here. ------------------- -- -- Bird -- ------------------- bird = display.newImage("images/bird.png"); bird.x = (\_W / 2) - 90; bird.y = \_H / 2; bird.xScale = 0.7; bird.yScale = 0.8; bird.collided = false; physics.addBody(bird,"static", {density = 0.1, friction = 0.5, bounce = 0.1, radius = 12}); group:insert(bird); birdIntro = transition.to(bird, {time = 2000, x = 100, onComplete = birdReady}); -------------------- -- -- End Bird -- -------------------- -------------------- -- -- Crashed Bird -- -------------------- crashedBird = display.newImage("images/crashed.png"); crashedBird.x = 100; crashedBird.y = 100; crashedBird.isVisible = false; group:insert(crashedBird); -------------------- -- -- End Crashed Bird -- -------------------- end -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) local group = self.view globalGroup = display.newGroup(); group:insert(globalGroup); -- INSERT code here (e.g. start timers, load audio, start listeners, etc.) --storyboard.purgeScene("menu"); --Purge the end game file if added storyboard.loadScene("menu"); --Runtime listener for the touch on the screen Runtime:addEventListener("touch", touchScreen); --Moves the cloud from right to left cloud.enterFrame = scrollCloud; Runtime:addEventListener("enterFrame", cloud); --Moves the cloud from right to left cloud2.enterFrame = scrollCloud; Runtime:addEventListener("enterFrame", cloud2); --Moves the top barricade from right to left topBar.enterFrame = scrollCloud; Runtime:addEventListener("enterFrame", topBar); --Moves the bottom barricade from right to left bottomBar.enterFrame = scrollCloud; Runtime:addEventListener("enterFrame", bottomBar); --Detects the collision Runtime:addEventListener("collision", onCollision); end -- Called when scene is about to move offscreen: function scene:exitScene( event ) local group = self.view bird:removeSelf(); gameBg:removeSelf(); cloud:removeSelf(); cloud2:removeSelf(); crashedBird:removeSelf(); topBar:removeSelf(); bottomBar:removeSelf(); topOfScreen:removeSelf(); bottomOfScreen:removeSelf(); group:removeSelf(); -- INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.) Runtime:removeEventListener("touch", touchScreen); Runtime:removeEventListener("enterFrame", bird); Runtime:removeEventListener("enterFrame", cloud); Runtime:removeEventListener("enterFrame", cloud2); Runtime:removeEventListener("enterFrame", topBar); Runtime:removeEventListener("enterFrame", bottomBar); Runtime:removeEventListener("collision", onCollision); --storyboard.removeAll ( ) end -- Called prior to the removal of scene's "view" (display group) function scene:destroyScene( event ) local group = self.view --storyboard.removeAll(); -- INSERT code here (e.g. remove listeners, widgets, save state, etc.) --bird:removeSelf(); -- INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.) Runtime:removeEventListener("touch", touchScreen); Runtime:removeEventListener("enterFrame", bird); Runtime:removeEventListener("enterFrame", cloud); Runtime:removeEventListener("enterFrame", cloud2); Runtime:removeEventListener("enterFrame", topBar); Runtime:removeEventListener("enterFrame", bottomBar); Runtime:removeEventListener("collision", onCollision); group = nil; end --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- "createScene" event is dispatched if scene's view does not exist scene:addEventListener( "createScene", scene ) -- "enterScene" event is dispatched whenever scene transition has finished scene:addEventListener( "enterScene", scene ) -- "exitScene" event is dispatched before next scene's transition begins scene:addEventListener( "exitScene", scene ) -- "destroyScene" event is dispatched before view is unloaded, which can be -- automatically unloaded in low memory situations, or explicitly via a call to -- storyboard.purgeScene() or storyboard.removeScene(). scene:addEventListener( "destroyScene", scene ) --------------------------------------------------------------------------------- return scene
@BrentSorrentino Here’s most of the code that would control the sprite’s movement. I tried setting the Velocity back to 0,0 but it had no effect on the sprite in the second play. Thanks for any help!
How do you reset the scene? I don’t see it here. When you go back to menu scene try removing the game scene so it destroys it and then recreates it. You are adding event listeners twice here. Once in create scene and once in enter scene. They should only be in inter scene. In createScene create objects and other thing in enterScene start things. In exitScene stop things in destroyScene clean up if necessary.
Thank you! You pointing out that I had added an eventlistener twice allowed me to figure it out! It was that my applyForce was so low compared to gravity so it had no real impact on the movement. I can’t believe I missed that! But thank you, I’ve been stuck on that for the past 4 days.