I am trying to create a game using gyroscope and I am facing a very strange problem. I have the following two scene’s
I want when the ball in the first scene collide with the door to move to the second scene.
When the ball move slowly and collide with the door everything works fine and the next scene starts normaly, but if the ball in the first scene move very fast and collide with the door with great force the next scene starts frοsted, it seems like the enterscene event is not triggered. Any Idea?
My code:
--------------------------------------------------------------------------------- -- -- 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's 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's "view" (display group) function scene:destroyScene( event ) print( "((destroying scene 1's view))" ) package.loaded[physics] = nil physics = 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
--------------------------------------------------------------------------------- -- -- 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's 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's "view" (display group) function scene:destroyScene( event ) print( "((destroying scene 2's view))" ) package.loaded[physics] = nil physics = 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