Hi,
i want to delete an Object completely after some time. When the object collides it changes its image. After 2 secounds it should be deleted. My first attempt is this:
function Trigger:init(xp, yp, world)
-- self:addChild(sprite) ...
local body = world:createBody{type = b2.DYNAMIC_BODY}
self.body.type = "Trigger"
self.parent = self
-- .....
self.body.aktiviert = false
self.body.killed = false
self.body:setLinearDamping(damping)
self:addEventListener(Event.ENTER_FRAME, function()
triggerAktiviert(self,world)
end)
end
function triggerAktiviert(self, world)
if self.body.aktiviert and self.body.killed == false then
if self.triggerSpr1 then self:removeChild(self.triggerSpr1) end
self:addChild(self.triggerSpr2)
self.triggerSpr2:setPosition(-50,-50)
self.body.killed = true
Timer.delayedCall(2000, function()
finishHim(self,world)
end)
end
end
function finishHim(self,world)
print("killing")
if self.body then world:destroyBody(self.body) end
if self.triggerSpr2 then self:removeChild(self.triggerSpr2) end
end |
This gives an error message: body allready destroyed in this function:
function Scene3:onEnterFrame()
self.world:step(1/60, 8, 3)
for i = 1, self:getNumChildren() do
local sprite = self:getChildAt(i)
if sprite.body then -- <<<<<--------
local body = sprite.body
local bodyX, bodyY = body:getPosition()
sprite:setPosition(bodyX, bodyY)
sprite:setRotation(body:getAngle() * 180 / math.pi)
end
end
end |
Am I right, that this error appears, because finishHim and Scene3:onEnterFrame are running parallel? And immidiately after if sprite.body (true) is checked the body gets destroyed in finishHim?
So the only way to do it is to delete the body in Scene3:onEnterFrame. if self.killed destroyBody and so on?
I just want to understand
Comments
trigger.lua:39: attempt to index field 'body' (a nil value)
It occures in function triggerAktiviert
But let me ask again: Do these functions really run parallel? Because if so, then body could get deleted after the other function checked if sprite.body then...
And what is on trigger.lua:39?
i tried to remove the EventListener that calls the function but still the same error?!
one small thing
With Gideros you can use some cool things and also pass data if you so wish.
so you could have the same as
you can also pass the world, and other data if you pass the same as a table like
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
trigger.lua:38: attempt to index field 'body' (a nil value)
stack traceback:
trigger.lua:38: in function
If not self.body then return end
That should prevent errors but tjere are probably cleaner ways to handle elsewhere in your code (like whatever calls that function most likely)
Likes: zvardin
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
if self.body.aktiviert and self.body.killed == false then
I am just guessing but if you are trying to use the and like & I think they do not work the same way like some other languages.
thx for the help. It is still not running. This prints out a "nil". I dont get it...
Likes: zvardin
But your hint workes too zvardin.