function scene:show(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then player:addEventListener("collision") Runtime:addEventListener("touch", onTouch) end end function scene:hide(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then print("hide removing objects") composer.removeScene( "start" ) player:removeEventListener("collision") Runtime:removeEventListener("touch", onTouch) display.remove( ball ) display.remove( player ) end end
Yes it is in scene create after the players code .
I tried that about 2 days ago for a different reason and when I did my computer began bugging out showing ads when nothing was open so I saved my files and reset my computer and I don’t wanna go back down that path . Is there another way I can send the project ?
@H.R.H, it was a very simple fix. All I did was move the addEventListener line in the restart.lua to the did phases:
function scene:show(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then Runtime:addEventListener("touch", touchScreen) composer.removeScene( "game" ) end end function scene:hide(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then composer.removeScene( "restart" ) Runtime:removeEventListener("touch", touchScreen) end end
Also, please, please format your code. (I said please twice for emphasis) When reading your code, it was difficult to tell the ends of function and such. I formatted your start.lua file, so please compare with restart.lua and do the same. If you format the code, it will be easier for people to help you.
In this download, I had to remove the images, otherwise, the file would have been too big to upload. Just copy your images folder over to the root of this folder. Make sure you do this or else you will get an error.
I am now trying to add a line that is placed behind the player so when the ball passes the player and touches the line , they will go to the next level but nothing happens when I touch the line . I stay in the game . Can someone help me ?
ball = display.newImage("ball1.png") ball.x = 400 ball.y = 160 physics.addBody(ball, "dynamic") screenGroup:insert(ball) line = display.newImage("line.png") line.x = 80 line.y = 200 line:scale( 0.1, 0.2 ) physics.addBody(line, "static", {density=.1, friction=.2, radius=12}) screenGroup:insert(line) player = display.newImage("player.png") player.x = 100 player.y = 300 physics.addBody(player, "static", {density=.1, bounce=0.2, friction=.2, radius=12}) screenGroup:insert(player) function player:collision(event) if event.phase == "began" then transition.cancel() composer.gotoScene("restart", options) end end function ball:onCollision(event) if event.phase == "began" then print("Touched") end end
function scene:show(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then ball:addEventListener( "onCollision", onCollision) player:addEventListener("collision") Runtime:addEventListener("touch", onTouch) end end function scene:hide(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then print("hide removing objects") composer.removeScene( "start" ) ball:removeEventListener( "onCollision", onCollision ) player:removeEventListener("collision") Runtime:removeEventListener("touch", onTouch) display.remove( ball ) display.remove( player ) end end
Emmm… first off, that’s not how you use a local collision function. The purpose of that is just to revolve around one object, instead, you should change it to a global collision and fit both the ball’s and player’s collision in there.
local function onCollision() if event.phase == "began" then if event.object1 == "player" and event.object2 == "ball" then end end end
Anyways, the ball’s collision function is printing Touched in the console instead of changing levels. Add the .gotoScene line in the ball’s collision function.