Add event listener error

This is a good example of just trying random things until something works rather than understanding what you’re doing and why. If you think it through logically, why would you put the code to create the player inside its own collision function?

I just did what you told me to do and still got an error :

local function playerCollision( event ) if event.phase == "began" then end end 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") ball.myName = "ball" screenGroup:insert(ball) player = display.newImage("player.png") player.x = 100 player.y = 300 player.myName = "player" 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=600, y=20, onComplete=loopObject }) end transition.to(player, { time=600, y=300, onComplete=objectDown }) end loopObject() end

error :

game.lua:93: attempt to call method 'addEventListener' (a nil value)

The start screen goes into the game but then that problem shows up .

Where are you calling addEventListener?

Btw, you don’t need playerCollision and player:collision. Choose one or the other.

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.