Runtime Error {SOLVED}

I am trying to remove an object after it has collided with another object and the output window gives me this error!!

Runtime error
/Users/brandxpress/Desktop/feedPenguin/main.lua:56: attempt to call method ‘removeSelf’ (a nil value)
stack traceback:
[C]: in function ‘removeSelf’
/Users/brandxpress/Desktop/feedPenguin/main.lua:56: in function ‘_listener’
?: in function <?:514>
?: in function <?:215>

I thinks it has to do with the a event listener that I have set up for that object… Here is the code that I am working with… ----->
[lua]display.setStatusBar (display.HiddenStatusBar)
–> Hides the status bar

local _W = display.contentWidth
local _H = display.contentHeight

local loqsprite = require(‘loq_sprite’)
display.setDefault(“background”, 255, 255, 255)

loqsprite.addScale(2)

local physics = require (“physics”)
physics.start()
physics.setGravity(0,0)
–physics.setDrawMode(“hybrid”)

local scene = display.newGroup()
local BG = display.newImageRect ( “penguinBackground.png”, 1024, 768 )
BG.x = _W/2; BG.y = _H/2
– scene
scene.floor = display.newRect ( scene, 0, 750, 1024, 10 )
scene.topWall = display.newRect ( scene, 0, 0, 1024, 10 )
scene.leftWall = display.newRect ( scene, 0, 5, 10, 1020 )
scene.rightWall = display.newRect ( scene, 1015, 5, 10, 1020 )

physics.addBody( scene.floor, “static”, props )
physics.addBody( scene.topWall, “static”, props )
physics.addBody( scene.leftWall, “static”, props )
physics.addBody( scene.rightWall, “static”, props )

local box = display.newRect (0, 0, 280, 280)
box.x = 150; box.y = 600
box:setFillColor (0, 255, 255)
box.myName = “box”
physics.addBody(box,“static”, { radius=30, density=0.8 })
box:addEventListener(“collision”, box)

local fish = display.newImageRect (“fish.png”, 68, 26)
fish.x = 900; fish.y = 600
fish.myName = “fish”
physics.addBody(fish,“dynamic”, { radius=30, density=0.8 })

local pond = display.newImageRect (“pond.png”, 597, 344)
pond.x = 750; pond.y = 620

local pfactory = loqsprite.newFactory(‘penguin’)
local penguin = pfactory:newSpriteGroup()
penguin.x = 100; penguin.y = 550

local function onComplete()
print(“remove me”)
box:removeSelf()
fish:removeSelf()
–fish:removeEventListener( “touch”, touch )
end

function box:collision(event)
if event.other.myName == “fish” then
print("!!FEED ME!!")
timer.performWithDelay ( 150, onComplete )
penguin:play(“aniPenguin eating”)
end
end

function touch( event )
if (event.phase == ‘began’) then
fish.touch = physics.newJoint( “touch”, event.target, event.x, event.y )
fish.touch:setTarget( event.x, event.y )
display.getCurrentStage():setFocus( event.target )
elseif (event.phase == ‘moved’ and fish.touch ~= nil) then
fish.touch:setTarget( event.x, event.y )
elseif (fish.touch ~= nil) then
fish.touch:removeSelf()
fish.touch = nil
display.getCurrentStage():setFocus( nil )
end

return true
end

fish:addEventListener( “touch”, touch )[/lua]

Thanks for any help!!!

[import]uid: 51459 topic_id: 22024 reply_id: 322024[/import]

A quick fix is to use

display.remove(object)

instead of removeSelf [import]uid: 84637 topic_id: 22024 reply_id: 87570[/import]

Oh thanks Danny!! I just fixed it another way right before you replied!!

[lua]display.setStatusBar (display.HiddenStatusBar)
–> Hides the status bar

local _W = display.contentWidth
local _H = display.contentHeight

local loqsprite = require(‘loq_sprite’)
display.setDefault(“background”, 255, 255, 255)

loqsprite.addScale(2)

local physics = require (“physics”)
physics.start()
physics.setGravity(0,0)
–physics.setDrawMode(“hybrid”)

local scene = display.newGroup()
local BG = display.newImageRect ( “penguinBackground.png”, 1024, 768 )
BG.x = _W/2; BG.y = _H/2
– scene
scene.floor = display.newRect ( scene, 0, 750, 1024, 10 )
scene.topWall = display.newRect ( scene, 0, 0, 1024, 10 )
scene.leftWall = display.newRect ( scene, 0, 5, 10, 1020 )
scene.rightWall = display.newRect ( scene, 1015, 5, 10, 1020 )

physics.addBody( scene.floor, “static”, props )
physics.addBody( scene.topWall, “static”, props )
physics.addBody( scene.leftWall, “static”, props )
physics.addBody( scene.rightWall, “static”, props )

local box = display.newRect (0, 0, 280, 280)
box.x = 150; box.y = 600
box:setFillColor (0, 255, 255)
box.myName = “box”
physics.addBody(box,“static”, { radius=30, density=0.8 })
box:addEventListener(“collision”, box)

local fish = display.newImageRect (“fish.png”, 68, 26)
fish.x = 900; fish.y = 600
fish.myName = “fish”
physics.addBody(fish,“dynamic”, { radius=30, density=0.8 })

local pond = display.newImageRect (“pond.png”, 597, 344)
pond.x = 750; pond.y = 620

local pfactory = loqsprite.newFactory(‘penguin’)
local penguin = pfactory:newSpriteGroup()
penguin.x = 100; penguin.y = 550

–local function onComplete()
– print(“remove me”)
– box:removeSelf()
– fish:removeSelf()
–fish:removeEventListener( “touch”, touch )
–end

function box:collision(event)
if event.other.myName == “fish” then
print("!!FEED ME!!")
box:removeSelf()
print(“remove me”)
fish:removeSelf()
timer.performWithDelay ( 150, onComplete )
penguin:play(“aniPenguin eating”)
end
end

function touch( event )
if (event.phase == ‘began’) then
fish.touch = physics.newJoint( “touch”, event.target, event.x, event.y )
fish.touch:setTarget( event.x, event.y )
display.getCurrentStage():setFocus( event.target )
elseif (event.phase == ‘moved’ and fish.touch ~= nil) then
fish.touch:setTarget( event.x, event.y )
elseif (fish.touch ~= nil) then
fish.touch:removeSelf()
fish.touch = nil
display.getCurrentStage():setFocus( nil )
end

return true
end

fish:addEventListener( “touch”, touch )[/lua]

THANKS AGAIN!! [import]uid: 51459 topic_id: 22024 reply_id: 87573[/import]

No problem.

Just so you know.

The only difference between display.remove and removeself is that display.remove first checks to see if the object isnt nil before trying to remove it [import]uid: 84637 topic_id: 22024 reply_id: 87576[/import]