object removing error

hi I’m getting a small error when i try to remove this object

[lua]
local bar3 = display.newRect(130,300,150,30)
physics.addBody(bar3,“static”)[/lua]

because of this code but the code thats causing a error is necessary for the app

[lua]local myListener = function( event )

if bar3.x < -10 then
bar3.x = 300
elseif bar3.x > 430 then
bar3.x = -40
transition.to(bar3,{time = 500, x=1100})

end

end

Runtime:addEventListener( “enterFrame”, myListener )[/lua]

the error I’m getting is

(Attempt to compare nil with number

File: game.lua

Line: 80

stack traceback:

game.lua:80: in function <game.lua:78>

?: in function <?:205>)

You could try making bar3 a global variable. I am not sure but I think you need 3 “end” statements. One for the function one for the if and one for the elseif. 

Never mind disregard my answer on the if statements.

i tried making bar3 global but no success

can anyone help me I’m stuck

I don’t see where you are trying to remove the object?

[lua] local function finalcollision( self, event )
if (event.phase == “began”) then

display.remove(crate)
display.remove(scoreNumber)
display.remove(timerNumber)
display.remove( ballOne )

if score > highScore then
_path = system.pathForFile( “highScore.txt”, system.DocumentsDirectory )
_file = io.open( _path, “w” )
_file:write( score )

io.close( _file )
_file = nil
_path = nil
end

_path = system.pathForFile( “playerScore.txt”, system.DocumentsDirectory )
_file = io.open( _path, “w” )
_file:write( score )

io.close( _file )
_file = nil

_path = nil
print(“error”)
composer.gotoScene(“gameOver”)

display.remove(bar3)

end
end

bar3.collision = finalcollision
bar3:addEventListener(“collision”, bar3)
[/lua]

thats where i try to remove it

It looks like the error is happening because you remove bar3 but you are not removing the “enterFrame” listener myListener (which is trying to change bar3.x after it has been removed.  

Try:

... composer.gotoScene("gameOver") Runtime:removeEventListener("enterFrame", myListener) display.remove(bar3)

thank you so much!!! thank you for the quick responses and the assistance i needed

You could try making bar3 a global variable. I am not sure but I think you need 3 “end” statements. One for the function one for the if and one for the elseif. 

Never mind disregard my answer on the if statements.

i tried making bar3 global but no success

can anyone help me I’m stuck

I don’t see where you are trying to remove the object?

[lua] local function finalcollision( self, event )
if (event.phase == “began”) then

display.remove(crate)
display.remove(scoreNumber)
display.remove(timerNumber)
display.remove( ballOne )

if score > highScore then
_path = system.pathForFile( “highScore.txt”, system.DocumentsDirectory )
_file = io.open( _path, “w” )
_file:write( score )

io.close( _file )
_file = nil
_path = nil
end

_path = system.pathForFile( “playerScore.txt”, system.DocumentsDirectory )
_file = io.open( _path, “w” )
_file:write( score )

io.close( _file )
_file = nil

_path = nil
print(“error”)
composer.gotoScene(“gameOver”)

display.remove(bar3)

end
end

bar3.collision = finalcollision
bar3:addEventListener(“collision”, bar3)
[/lua]

thats where i try to remove it

It looks like the error is happening because you remove bar3 but you are not removing the “enterFrame” listener myListener (which is trying to change bar3.x after it has been removed.  

Try:

... composer.gotoScene("gameOver") Runtime:removeEventListener("enterFrame", myListener) display.remove(bar3)

thank you so much!!! thank you for the quick responses and the assistance i needed