Thanks a whole lot .
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.
I tried this :
function scene:create(event) local screenGroup = self.view local background = display.newImageRect("terrain.jpg",display.contentWidth,display.contentHeight) background.x = display.contentCenterX background.y = display.contentCenterY screenGroup:insert(background) 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 local function loopObject() local objectDown= function() transition.to(player, { time=1600, y=20, onComplete=loopObject }) end transition.to(player, { time=1600, y=300, onComplete=objectDown }) end loopObject() end
local function onCollision() if event.phase == "began" then if event.object1 == "ball" and event.object2 == "line" then print("Touched") end 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
When I tried the above code , nothing happened I didn’t get any errors . And then I did this code below :
ball:addEventListener( "onCollision") ball:removeEventListener( "onCollision")
And I got this error :
ERROR: Runtime error addEventListener: listener cannot be nil: nil stack traceback: [C]: in function 'error' 10:58:37.860 ?: in function 'getOrCreateTable' 10:58:37.860 ?: in function 'addEventListener' 10:58:37.860 ?: in function 'addEventListener' 10:58:37.860 ?: in function 'addEventListener' 10:58:37.860 C:\Users\user\Documents\Corona Projects\app\game.lua:100: in function '?'
line 100 :
ball:addEventListener( "onCollision")
Clearly, you are having trouble understanding event listeners, I suggest you look through this tutorial in depth.
I tried this and still got no response :
local function onLocalCollision( self, event ) if(event.phase == "ended") 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.collision = onLocalCollision ball:addEventListener( "collision" ) 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.collision = onLocalCollision ball:removeEventListener( "collision" ) player:removeEventListener("collision") Runtime:removeEventListener("touch", onTouch) display.remove( ball ) display.remove( player ) end end
I also tried it how it was in the example(s) but nothing changed .
hello