Add event listener error

I’m going to use player:collision 

This is where I am calling the event listener :

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

Answer these questions:

  1. Is the player made in scene: create()?

  2. Is the player: collision() function within scope of the player? (is it in scene: create after the player’s code?)

These are the cases in which a zip file would be ideal. Can you try to find a zip software?

Yes the player is made in the create scene .

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 ?

Upload it through GitHub, and put the link here.

How do I give you the zipped file ?

Put it on GitHub and place the link here. 

https://help.github.com/articles/create-a-repo/

Here’s the link

 https://github.com/HRH23/coronaa

Did you see it ?

Emm… There is no main.lua file. Corona needs a main.lua file to run.

Just posted the main.lua file .

There is no start.jpg file. 

Look, if it is going to have any errors other than the one you discussed in this post, I advise you review the project who posted to GitHub.

I forgot to add that one i’ll do it now .

I just updated the project .

Thanks for updating it. I will try to help tomorrow.

Ok 

Any luck with the problem ?

@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. :slight_smile:

Here’s a guide to help out as well: https://docs.coronalabs.com/tutorial/basics/codeFormatting/index.html

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.

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.