Hey all,
I am currently making a game where you move a ball by tilting your device and you must make the ball land on a certain platform. Once there is contact between the 2 objects, the platform teleports to a random position on screen and the ball bounces so you must make it land on the platform again, and again. You gain one point when colliding with the platform and you lose when the ball falls to the ground. Here is the actual level scene code (called “level1”) (I know the code is too long but please bear with me)
local composer = require( "composer" ) local scene = composer.newScene() local physics = require "physics" physics.start( ) function scene:create( event ) local sceneGroup = self.view end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen elseif phase == "did" then -- Called when the scene is now on screen -- -- INSERT code here to make the scene come alive -- e.g. start timers, begin animation, play audio, etc. local score = 0 local scoreText = display.newText( "Score: " .. score, 40, 0, native.systemFontBold, 20 ) scoreText:setFillColor( 1, 1, 1 ) local function platformCollision(self, event) if event.phase == "ended" then if event.target.type == "platform" and event.other.type == "ball" then timer.performWithDelay(1, function() self.x = math.random( 0, 300 ) self.y = math.random( 50, 300 ) self:setFillColor( math.random(0,1), math.random(0,1), math.random(0,1) ) end ) media.playSound( "jump.wav" ) end end end local function limitCollision(self, event) if event.phase == "ended" then if event.target.type == "limit" and event.other.type == "ball" then self:removeSelf( ) self = nil native.showAlert( "", "You scored " .. score .. " !" ) composer.removeScene( "level1", false ) composer.gotoScene( "menu", "fade", 500 ) end end end function GlobalCollision(event) if event.phase == "began" then if event.object1.type == "platform" and event.object2.type == "ball" then score = score + 1 scoreText.text = "Score: " .. score end end end local ball = display.newCircle( 100, 10, 10 ) ball.type = "ball" ball:setFillColor( 1,1,1 ) physics.addBody( ball, "dynamic", { bounce = 1 } ) local platform = display.newRect( 100, 100, 50, 5 ) physics.addBody( platform, "static" ) platform.type = "platform" platform.collision = platformCollision platform.strokeWidth = 1 local limit = display.newRect( display.actualContentWidth/2, display.actualContentHeight-150, display.actualContentWidth\*3 , 10 ) limit:setFillColor( 0,0,0 ) physics.addBody( limit, "static") limit.type = "limit" limit.collision = limitCollision local exit = display.newImage( "exxit.png", 160, 460 ) local function onAccelerate(event) ball.x = ball.x + event.xGravity \* 90 end local function exitf() native.showAlert( "", "You scored " .. score .. " !" ) composer.removeScene( "level1", false ) composer.gotoScene( "menu", "fade", 500 ) return true end exit:addEventListener( "touch", exitf ) limit:addEventListener( "collision", limit ) platform:addEventListener( "collision", platform ) Runtime:addEventListener( "collision", GlobalCollision ) Runtime:addEventListener("accelerometer", onAccelerate) sceneGroup:insert(platform) sceneGroup:insert(scoreText) sceneGroup:insert(ball) sceneGroup:insert(limit) sceneGroup:insert(exit) end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) elseif phase == "did" then -- Called when the scene is now off screen platform:removeSelf( ) platform = nil end end function scene:destroy( event ) -- Called prior to the removal of scene's "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc. local sceneGroup = self.view end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) ----------------------------------------------------------------------------------------- return scene
I tested the game on my Android and discovered quite a weird behavior:
When playing the game, the score stays 0 when colliding, when I lose, the game returns to menu but when I tap “Play” again and replay the level, the ball bounces without colliding with the actual platform, it seems like it is bouncing on invisible platforms. And the more I restart the level (without quitting the app) the more frequent the “invisible platforms”. What’s funny is that the score actually goes up when the ball collides with an “invisible plarform” but remains the same when colliding with the actual platform.
I seriously need someone to help me
Thank you