EnterScene event does not fire

I am trying to create a game using a gyroscope, and I ran into a very strange problem. I have the following two scenes

enter image description here

enter image description here

I want the ball in the first scene to collide with the door to go to the second scene. When the ball moves slowly and collides with the door, everything works fine, and the next scene starts fine, but if the ball in the first scene moves very fast and pushes against the door with great force, the next scene begins, it seems that this western event does not start. Any idea?

My code is:

SCENE1:

--------------------------------------------------------------------------------- -- -- scene1.lua -- --------------------------------------------------------------------------------- local storyboard = require( "storyboard" ) local scene = storyboard.newScene() system.setIdleTimer( false ) local physics = require "physics" local physicsData = (require "myphysics").physicsData(1.0) --------------------------------------------------------------------------------- -- BEGINNING OF IMPLEMENTATION --------------------------------------------------------------------------------- local displayTime,background,ball,maze,maze2,borders,exitscn local startTime=0 local levelTime = 20 local function onGyroscopeDataReceived( event ) local deltaRadiansX = event.xRotation * event.deltaTime local deltaDegreesX = deltaRadiansX * (180 / math.pi) local deltaRadiansY = event.yRotation * event.deltaTime local deltaDegreesY = deltaRadiansY * (180 / math.pi) ball:applyForce( -deltaDegreesX*6, -deltaDegreesY*6, ball.x, ball.y ) end function nextScene() storyboard.gotoScene( "loadscene2") end local function onCollision( event ) if ( event.phase == "ended" ) then if(event.object1.name=="exitscn" or event.object2.name=="exitscn") then timer.performWithDelay ( 500, nextScene ) end end end local function gameOver() storyboard.gotoScene( "menu", "fade", 300) end local function checkTime(event) local now = os.time() displayTime.text = levelTime - (now - startTime) if ( levelTime - (now - startTime)==0) then gameOver() end end -- Called when the scene view does not exist: function scene:createScene( event ) local screenGroup = self.view physics.start(); physics.setGravity( 0,0 ) displayTime = display.newText(levelTime, 0, 0, "Helvetica", 20) displayTime.isVisible=false background=display.newImage("bcklevel1.png") background.x=display.contentCenterX background.y=display.contentCenterY ball=display.newImage("ball1.png") ball.x=30 ball.y=display.contentCenterY ball.name="ball" maze=display.newImage( "maze1.png" ) maze.x=display.contentCenterX maze.y=display.contentCenterY maze.name="maze" maze2=display.newImage( "maze1.png" ) maze2.x=display.contentCenterX maze2.y=display.contentCenterY maze2.name="maze2" borders=display.newImage( "borders.png" ) borders.x=display.contentCenterX borders.y=display.contentCenterY borders.name="borders" borders.alpha=0.1 exitscn=display.newImage("exit.png") exitscn.x=display.contentWidth-30 exitscn.y=display.contentCenterY exitscn.name="exitscn" physics.addBody (ball, "dynamic",physicsData:get("ball")) physics.addBody (maze, "static",physicsData:get("mazelevel1_1")) physics.addBody (maze2, "static",physicsData:get("mazelevel1_2")) physics.addBody (borders, "static",physicsData:get("borders")) physics.addBody (exitscn, "dynamic",physicsData:get("exitscn")) --ball:addEventListener ( "touch", nextScene ) Runtime:addEventListener("enterFrame", checkTime) Runtime:addEventListener( "gyroscope", onGyroscopeDataReceived ) Runtime:addEventListener( "collision", onCollision ) screenGroup:insert( background ) screenGroup:insert(displayTime) screenGroup:insert( ball ) screenGroup:insert( maze ) screenGroup:insert( maze2 ) screenGroup:insert( borders ) screenGroup:insert( exitscn ) print( "\n1: createScene event") end -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) print( "1: enterScene event" ) physics.start() startTime = os.time() displayTime.isVisible=true end -- Called when scene is about to move offscreen: function scene:exitScene( event ) print( "1: exitScene event" ) physics.stop( ) Runtime:removeEventListener( "enterFrame", checkTime ) Runtime:removeEventListener( "gyroscope", onGyroscopeDataReceived ) Runtime:removeEventListener( "collision", onCollision ) end -- Called prior to the removal of scene "view" (display group) function scene:destroyScene( event ) print( "((destroying scene 1 view))" ) package.loaded[physics] = nil physics = nil end --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- "createScene" event is dispatched if scene 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 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 

SCENE 2:

 --------------------------------------------------------------------------------- -- -- scene2.lua -- --------------------------------------------------------------------------------- local storyboard = require( "storyboard" ) local scene = storyboard.newScene() system.setIdleTimer( false ) local physics = require "physics" local physicsData = (require "myphysics").physicsData(1.0) --------------------------------------------------------------------------------- -- BEGINNING OF IMPLEMENTATION --------------------------------------------------------------------------------- local displayTime,background,ball,maze,maze2,borders,exitscn local startTime=0 local levelTime = 20 local function onGyroscopeDataReceived( event ) local deltaRadiansX = event.xRotation * event.deltaTime local deltaDegreesX = deltaRadiansX * (180 / math.pi) local deltaRadiansY = event.yRotation * event.deltaTime local deltaDegreesY = deltaRadiansY * (180 / math.pi) ball:applyForce( -deltaDegreesX*6, -deltaDegreesY*6, ball.x, ball.y ) end function nextScene() storyboard.gotoScene( "menu", "fade", 1000 ) end local function onCollision( event ) if ( event.phase == "ended" ) then if(event.object1.name=="exitscn" or event.object2.name=="exitscn") then timer.performWithDelay ( 500, nextScene ) end end end local function gameOver() storyboard.gotoScene( "menu", "fade", 300) end local function checkTime(event) local now = os.time() displayTime.text = levelTime - (now - startTime) if ( levelTime - (now - startTime)==0) then gameOver() end end -- Called when the scene view does not exist: function scene:createScene( event ) local screenGroup = self.view physics.start(); physics.setGravity( 0,0 ) displayTime = display.newText(startTime, 0, 0, "Helvetica", 20) displayTime.isVisible=false background=display.newImage("bcklevel1.png") background.x=display.contentCenterX background.y=display.contentCenterY ball=display.newImage("ball1.png") ball.x=30 ball.y=display.contentCenterY ball.name="ball" maze=display.newImage( "maze2.png" ) maze.x=display.contentCenterX maze.y=display.contentCenterY maze.name="maze" maze2=display.newImage( "maze2.png" ) maze2.x=display.contentCenterX maze2.y=display.contentCenterY maze2.name="maze2" borders=display.newImage( "borders.png" ) borders.x=display.contentCenterX borders.y=display.contentCenterY borders.name="borders" borders.alpha=0.1 exitscn=display.newImage("exit.png") exitscn.x=display.contentWidth-30 exitscn.y=display.contentCenterY exitscn.name="exitscn" physics.addBody (ball, "dynamic",physicsData:get("ball")) physics.addBody (maze, "static",physicsData:get("mazelevel2_1")) physics.addBody (maze2, "static",physicsData:get("mazelevel2_2")) physics.addBody (borders, "static",physicsData:get("borders")) physics.addBody (exitscn, "dynamic",physicsData:get("exitscn")) --ball:addEventListener ( "touch", nextScene ) Runtime:addEventListener("enterFrame", checkTime) Runtime:addEventListener( "gyroscope", onGyroscopeDataReceived ) Runtime:addEventListener( "collision", onCollision ) screenGroup:insert( background ) screenGroup:insert(displayTime) screenGroup:insert( ball ) screenGroup:insert( maze ) screenGroup:insert( maze2 ) screenGroup:insert( borders ) screenGroup:insert( exitscn ) print( "\n1: createScene event") end -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) print( "1: enterScene event" ) physics.start() startTime = os.time() displayTime.isVisible=true end -- Called when scene is about to move offscreen: function scene:exitScene( event ) print( "1: exitScene event" ) physics.stop( ) Runtime:removeEventListener( "enterFrame", checkTime ) Runtime:removeEventListener( "gyroscope", onGyroscopeDataReceived ) Runtime:removeEventListener( "collision", onCollision ) end -- Called prior to the removal of scene "view" (display group) function scene:destroyScene( event ) print( "((destroying scene 2 view))" ) package.loaded[physics] = nil physics = nil end --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- "createScene" event is dispatched if scene 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 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 
+4
source share
4 answers

In the second file, I'm sure there is something strange about

 function nextScene() storyboard.gotoScene( "menu", "fade", 1000 ) end local function onCollision( event ) if ( event.phase == "ended" ) then if(event.object1.name=="exitscn" or event.object2.name=="exitscn") then timer.performWithDelay ( 500, nextScene ) end end end 

first you delay 500 ms, and then an additional 1000 ms delay the transition to the next scene. You may be aware of this, but you are not handling this transition in the same way as in the first file.

0
source

I think the code is correct. There is some problem of grouping things when entering a scene and reloading memory for scenic scenes. Try the following solutions:

  • I think that you wrote the whole code in the creation of the scene, and this will call the scene once for the storyboard. You should try to implement reusable code in enterScene and should use the local group = self.view to input scene objects.

  • You should try to update or free the memory of the current scene in the script, either in a DOOR collision with the ball OR from the exit scene of the current storyboard.

0
source

I think the problem is that the collision event is fired more than once while you delay the nextScene call.

Try to do something like this:

 local hasNextSceneEventHappened = false local function onCollision( event ) if ( event.phase == "ended" ) then -- Ignore the call if we have already collided for next level if(event.object1.name=="exitscn" or event.object2.name=="exitscn") then if (hasNextSceneEventHappened == true ) then return end hasNextSceneEventHappened = true timer.performWithDelay ( 500, nextScene ) end end end 

This code ensures that you do not spam goToScene () and get unwanted behavior. Hope this works for you.

0
source

use a flag like didGo . run this flag in your file body to false and write something like this:

 -- body of your file: local didGo = false -- when you want to go to another scene if ( not didGo) then didGo = true storyboard.gotoScene... end 
0
source

Source: https://habr.com/ru/post/1490136/


All Articles