Attempt to perform arithmetic on field 'rotation' (a nil value)

Hi, I am having trouble with the thrusters for a rocket. Once the rocket has been destroyed, occasionally an error appears that says “Attempt to perform arithmetic on field ‘rotation’ (a nil value)”. I believe it may have something to do with the rocket being nil when the necessary calculations are performed. Here is the collision code and the thruster calculation code:

rocketCollision = function(self, event) local other = event.other other.health = other.health - self.damage self.health = self.health - other.damage if event and event.phase == "began" then if self.health \< 0 then explosion.new(self.parent, self.x, self.y, 0.5, 0.5, math.random(-20, 20), math.random(-20, 20)) ignore("enterFrame", self) display.remove(self.thruster) display.remove(self) end end end moveThruster = function(self) local thruster = self.thruster local vec1 = angle2Vector( self.rotation + 180, true) \<--error line vec1 = vecNorm( vec1 ) vec1 = vecScale( vec1, thruster.yOffset ) vec1 = vecAdd( vec1, self ) local vec2 = angle2Vector( self.rotation + 90, true ) vec2 = vecNorm( vec2 ) vec2 = vecScale( vec2, thruster.xOffset ) thruster.rotation = self.rotation thruster.x = vec1.x + vec2.x thruster.y = vec1.y + vec2.y end

Any help would be great, thank you!

Update: the error occurs whenever the rocket is removed. I know what is going on now…

Nope that did not work. I tried this:

 rocket.timer = display.remove timer.performWithDelay(4000, rocket) rocket.thruster.timer = display.remove timer.performWithDelay(3500, rocket.thruster)

I also tried adding a finalize listener to rocket, but that did not work:

rocket.finalize = finalize1 listen("finalize", rocket) finalize1 = function(self) ignore("enterFrame", self) end

Wrong order, and you keep making the same mistake with listen() …

Remember, listen( event, obj ) is shorthand for Runtime:addEventListener( event, obj )

Finalize is NOT a Runtime event.  :slight_smile:

local finalize1 = function(self) ignore("enterFrame", self) end rocket.finalize = finalize1 --listen("finalize", rocket) -- WRONG! rocket:addEventListener( "finalize" )

Thank you. This worked perfectly.

I feel like I may have goofed you up by introducing you to SSK2’s shorthand listen() and ignore() functions.

Whenever you are about to use listen(), go read the API docs about the event to be sure it isn’t meant to be set up with obj:addEventListener() or you will keep having these problems.

https://docs.coronalabs.com/api/event/index.html

Better yet, take a timeout and make a list of Runtime events and local events (those that use obj:addEventListener)

Then, only use listen() for Runtime events. :slight_smile:

Now I feel as if the difference is more profound, thank you.

Update: the error occurs whenever the rocket is removed. I know what is going on now…

Nope that did not work. I tried this:

 rocket.timer = display.remove timer.performWithDelay(4000, rocket) rocket.thruster.timer = display.remove timer.performWithDelay(3500, rocket.thruster)

I also tried adding a finalize listener to rocket, but that did not work:

rocket.finalize = finalize1 listen("finalize", rocket) finalize1 = function(self) ignore("enterFrame", self) end

Wrong order, and you keep making the same mistake with listen() …

Remember, listen( event, obj ) is shorthand for Runtime:addEventListener( event, obj )

Finalize is NOT a Runtime event.  :slight_smile:

local finalize1 = function(self) ignore("enterFrame", self) end rocket.finalize = finalize1 --listen("finalize", rocket) -- WRONG! rocket:addEventListener( "finalize" )

Thank you. This worked perfectly.

I feel like I may have goofed you up by introducing you to SSK2’s shorthand listen() and ignore() functions.

Whenever you are about to use listen(), go read the API docs about the event to be sure it isn’t meant to be set up with obj:addEventListener() or you will keep having these problems.

https://docs.coronalabs.com/api/event/index.html

Better yet, take a timeout and make a list of Runtime events and local events (those that use obj:addEventListener)

Then, only use listen() for Runtime events. :slight_smile:

Now I feel as if the difference is more profound, thank you.