Help with collision needed

Please read the docs before you post here. It’s clearly stated that you need to supply BOTH x and y gravity values. I personally make sure that the Corona docs are as accurate as possible so that these type of questions don’t need to be asked, so please read them and learn from them.

Best regards,

Brent

Thanks it works now.

Faceslap.

It’s because there is nothing going on in your collision function. It just prints values, you have to put the code inside the onGlobalCollision function. Actually, a local collision function would probably be better to use on the player.

Like this ?

local function onPlayerCollision( event ) if event.phase =="began" then composer.gotoScene("restart", options) end end

Yes, and make sure to add the eventListener:

player:addEventListener("collision")

This is what I have :

local function onPlayerCollision( event ) if event.phase =="began" then composer.gotoScene("restart", options) end end function scene:show(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then print("show started") player:addEventListener("onPlayerCollision") elseif ( phase == "did" ) then print("show showing objects") Runtime:addEventListener("touch", onTouch) end end function scene:hide(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then print("hide started") composer.removeScene( "start" ) player:removeEventListener("onPlayerCollision") elseif ( phase == "did" ) then print("hide removing objects") Runtime:removeEventListener("touch", onTouch) end end

And I’m constantly getting this error :

ERROR: Runtime error 11:49:33.912 addEventListener: listener cannot be nil: nil 11:49:33.912 stack traceback: 11:49:33.912 [C]: in function 'error' 11:49:33.912 ?: in function 'getOrCreateTable' 11:49:33.912 ?: in function 'addEventListener' 11:49:33.912 ?: in function 'addEventListener' 11:49:33.912 ?: in function 'addEventListener' 11:49:33.912 C:\Users\user\Documents\Corona Projects\app\game.lua:96: in function '?' 11:49:33.912 ?: in function 'dispatchEvent' 11:49:33.912 ?: in function '\_nextTransition' 11:49:33.912 ?: in function \<?:1492\> 11:49:33.912 (tail call): ? 11:49:33.912 ?: in function '?' 11:49:33.912 ?: in function \<?:182\> 11:49:35.521 

This is line 96 :

player:addEventListener("onPlayerCollision")

I also tried :

 player:addEventListener("onCollision")

Sorry my bad:

player.collision = onPlayerCollision player:addEventListener("collision")

Nearly there.  You’ve told it to look for a collision, but not where to go when it detects one.  Look in the API refs for event listeners and read the bit about how to call them properly…

You can also do:

player:addEventListener("collision", onPlayerCollision)

I have this :

local function onPlayerCollision( 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 print("show started") elseif ( phase == "did" ) then print("show showing objects") player:addEventListener("collision", onPlayerCollision) Runtime:addEventListener("touch", onTouch) end end function scene:hide(event) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then print("hide started") composer.removeScene( "start" ) elseif ( phase == "did" ) then print("hide removing objects") player:addEventListener("collision", onPlayerCollision) Runtime:removeEventListener("touch", onTouch) end end

And now nothing is happening no errors or anything

First of all on if phase == “did”, you have to remove the collision listener, not add it again.

Also, when you say nothing is happening, did the player ever collide with anything?

Try replacing this:

player:addEventListener("collision", onPLayerCollision)

with this:

player.collision = onPlayerCollision player:addEventListener("collision")

Yes the player collided with the ball . 

I have this now :

local function onPlayerCollision( 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 print("show started") elseif ( phase == "did" ) then print("show showing objects") player.collision = onPlayerCollision 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 print("hide started") composer.removeScene( "start" ) elseif ( phase == "did" ) then print("hide removing objects") player.collision = onPlayerCollision player:removeEventListener("collision") Runtime:removeEventListener("touch", onTouch) end end

And still nothing happened . The console didn’t print anything .

The line that says: 

local function onPLayerCollision(event)

Add self as a parameter too:

local function onPlayerCollision(self, event)

Still nothing in the console :

local function onPlayerCollision( self, event ) if event.phase =="began" then print("Touched") end end

Let me know if you need more code .

Really? Can you send me your project zipped up?

I don’t have zip . I hate it .

But I can give you the code in my game.lua