bad argument#-1 to '_listener'(Proxy expected, got nil)

Hi everyone, I’ve encountered an error when I was running my code:bad argument#-1 to ‘_listener’(Proxy expected, got nil)…in function ‘_listener’…<?:141>…<?:218>

This is the first time I’ve encountered this error and I believe its related to the collision function I’ve been working on.

function scene:createScene()

local group = self.view

function shot:touch(event)

    if(event.phase == ‘ended’) then

     

        Runtime:removeEventListener(‘enterFrame’, charge)

        cannon.isVisible = true

        cannon.rotation = 0

        

        cannonBall = display.newImage(‘cannon ball.png’, 84, 220)

        physics.addBody(cannonBall, {density = 1, friction = 0, bounce = 0})

        group:insert(cannonBall)-----------------------> this is line 141

        print (‘shot’)

     

– Shoot cannon ball

cannonBall:applyLinearImpulse(3, impulse, cannonBall.x, cannonBall.y )

–Collision listener

print(‘event listener’)

cannonBall:addEventListener (‘collision’, ballCollision)

    

    end

end

function charge()

local degreesPerFrame = 1

cannon.rotation = cannon.rotation - degreesPerFrame 

impulse=impulse-0.2

    

     if(cannon.rotation < -46) then

          cannon.rotation = -46

          impulse = -3.2

          print(‘Charge’)

        end

end

function ballCollision(event)

   if (event.other == balloons[3])  then

        scene.updateScore()

        print(‘Ball is colliding’)

        timer.performWithDelay(1, event.target.removeSelf, event.target) 

        timer.performWithDelay(1, event.other.removeSelf,  event.target) 

        balloonText3.isVisible = false – this is ok: not involved in collision

        audio.play(pop)

    end

end

end

return scene --------> this is line 218

Are you removing the scene prior to going to this scene? 

You mean the previous scene which is the menu? I have this code at the start:
 

local storyboard = require( “storyboard” )

local widget = require "widget"

local scene = storyboard.newScene()

local physics = require(‘physics’)

physics.start()

– Clear previous scene

storyboard.removeAll()

Change your createScene to enterScene and tell me what happens :slight_smile:

I had 2 eventlisteners in enterScene and I moved them to createScene and changed createScene to enterScene, after that i also created a new createScene which has nothing except local group = self.view in it. When i run the code it gave me the same error as above. 

Could it be because the collision listener is at the wrong place?

‘Proxy expected, got nil’ - your function expected proxy as argument (image) but got nil. It means that cannonBall is not display object but nil.

Why it’s called proxy? It’s because from Lua code images are tables with image data atached. It’s under .__proxy property of display object (table), thats why error calls it ‘proxy’. Of course cannot do anything with it from Lua code, but Corona uses it underneath.

So it detects nothing even though its an image? Is there anyway to fix it?

Are you sure there is image? Because it’s nil then it seems it failed to create it.

If is then the reason can be listener.

On collision you call removeSelf() which only removea image data (proxy) but still leaves skeleton table, so you must also nil object.

If you have code as you posted then another problem is that you add collision listener with function you declare underneath, which totaly wrong.

What’s more

[lua]
When the event is detected with a table listener within an object, certain properties are replaced:
object1 → target
object2 → other
element1 → selfElement
element2 → otherElement
[/lua]

You do not use this type of listener so your collision function doesn’t have event.target or event.other but event.object1 and event.object2.

It’s all I can see wrong / potentially wrong in posted code. Tell me if something will help :slight_smile:

So i put e.other = nill and e.target = nill?

There is no other and target according to documentation.

What do you mean by nil object then?

Are you removing the scene prior to going to this scene? 

You mean the previous scene which is the menu? I have this code at the start:
 

local storyboard = require( “storyboard” )

local widget = require "widget"

local scene = storyboard.newScene()

local physics = require(‘physics’)

physics.start()

– Clear previous scene

storyboard.removeAll()

Change your createScene to enterScene and tell me what happens :slight_smile:

I had 2 eventlisteners in enterScene and I moved them to createScene and changed createScene to enterScene, after that i also created a new createScene which has nothing except local group = self.view in it. When i run the code it gave me the same error as above.