issue with

hi guys. I have this little game that I have been working on. I’m stuck on this particular issue for 2 days now.

This is what’s happening. I spawn a dragon and move it across the X axis and when it reaches a certain point, I want to remove it. The dragon is a
sprite.

When the dragon is remove and nil, I keep getting this error.

…HMzBF-CWYcERpFamRE+++TI/TemporaryItems/21/level1.lua:267: in function <…hmzbf-cwycerpfamre>
?: in function <?:214>
Runtime error
…HMzBF-CWYcERpFamRE+++TI/TemporaryItems/21/level1.lua:267: attempt to index upvalue ‘greendragonInstance’ (a nil value)
stack traceback:
[C]: ?

all I want to do is remove the dragon.
here’s my code:

Thanks in advance for any helps.

[lua] – level objects goes in here

local createDragons = function()
greendragonInstance = sprite.newSprite(greendragonSet)
greendragonInstance.x = 550
greendragonInstance.y = mRand(50, 120)
greendragonInstance.dx = 2
greendragonInstance.dy = mRand( -1, 1)
greendragonInstance.objectName = “greendragonInstance”
greendragonInstance.isVisible = true

physics.addBody(greendragonInstance, “static”, { density = 0, friction = 0, bounce = 0 })

levelGroup:insert(greendragonInstance)
gameGroup:insert(levelGroup)

greendragonInstance:prepare(“greendragon”)
greendragonInstance:play()

end
–===================== Move Dragon ===================================================
local movedragon = function ()

greendragonInstance.x = greendragonInstance.x - greendragonInstance.dx

end
–====================== Destroy Dragons ==============================================
local destroydragon = function ()

if greendragonInstance.x <= 300 then – line 267 is here
greendragonInstance.x = greendragonInstance.x
greendragonInstance.isVisible = false

Runtime:removeEventListener(“enterFrame”, movedragon)
greendragonInstance:removeSelf()
greendragonInstance = nil
end

end

local canceldestroydragon = function ()
Runtime:removeEventListener(“enterFrame”, destroydragon)
end

local destroydragonTimer = timer.performWithDelay ( 1, destroydragon, 1)
local trancancel = transition.to(canceldestroydragon, { time = 2, delay = 1, onComplete = destroydragonTimer})
[import]uid: 12455 topic_id: 5611 reply_id: 305611[/import] </…hmzbf-cwycerpfamre>

Without even reading your code that error looks like an event listener is still trying to use the dragon after you removed it. Notice this line says something in the program is trying to use the variable you set to nil:
“attempt to index upvalue ‘greendragonInstance’ (a nil value)”

When you remove the dragon make sure to also cancel any event listeners that use it.

Also, that error appears to be pointing to line 267 (presumably the code you’ve posted here is just a small excerpt from a larger program) so that’s the first place to look for a problem. The second place to look is the line preceding that.

ADDITION: Skimming through your code I am pretty confused by your last lines. Like, why do you use removeEventListener for the destroyDragon function? Did you set an enterFrame listener with that? Why?

Also, the syntax of your last line looks totally wrong. You call a transition on a function and not a display object, and then the onComplete points to a timer and not a function.

What are you attempting to to there anyway? [import]uid: 12108 topic_id: 5611 reply_id: 19133[/import]

Thanks for your suggestion, I will look into it. [import]uid: 12455 topic_id: 5611 reply_id: 19134[/import]

Ok I fixed the dragon issue. here’s the code in case someone has similar issue.

[lua] greendragon = sprite.newSpriteSheetFromData ( “greendragon.png”, require(“greendragon”).getSpriteSheetData() )
greendragonSet = sprite.newSpriteSet( greendragon, 1, 4 )
sprite.add( greendragonSet, “greendragon”, 1, 4, 1000, 0 )

–**************************************************************************************************
– level objects goes in here
local grndragons = {}
local createDragons = function()
greendragonInstance = sprite.newSprite(greendragonSet)
greendragonInstance.x = 550
greendragonInstance.y = mRand(50, 120)
greendragonInstance.dx = 2
greendragonInstance.dy = mRand( -1, 1)
greendragonInstance.objectName = “greendragonInstance”
greendragonInstance.isVisible = true

physics.addBody(greendragonInstance, “static”, { density = 0, friction = 0, bounce = 0 })

levelGroup:insert(greendragonInstance)
gameGroup:insert(levelGroup)

greendragonInstance:prepare(“greendragon”)
greendragonInstance:play()

grndragons[#grndragons + 1] = greendragonInstance

end
–===================== Move Dragon ===================================================

local movedragon = function ()
local newDragonX, grndragon

for b=#grndragons, 1, -1 do

grndragon = grndragons[b]
newDragonX = grndragon.x - grndragon.dx

if(newDragonX < 5 )
then
grndragon:removeSelf()
table.remove(grndragons, b)
print(grndragons, #grndragons)

else
grndragon.x = newDragonX
end
end
end

Runtime:addEventListener(“enterFrame”, movedragon) [import]uid: 12455 topic_id: 5611 reply_id: 19170[/import]