Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Remove body and keep sprite — Gideros Forum

Remove body and keep sprite

DikkesnoekDikkesnoek Member
edited April 2013 in General questions
Hi,

I've again found something I don't completely understand. During gameplay, I sometimes want to destroy a body and sprite (in a for body,sprite in pairs(actors) do loop) by using:
if (sprite.removeMe) then
	world:destroyBody(body)
	stage:removeChild(sprite)
	actors[body] = nil 
end
and sometimes I will keep the picture/sprite in place and only deactivate the body by using:
if (sprite.removeMyBody) then
	body:setActive(false)
	sprite.removeMyBody = false
end
Sometimes I will get an error:
main.lua:190: Body is already destroyed.

Instead of body:setActive(false), destroying the body is also possible but gives me more errors.

Is there a proper way to determine if a body has removed from the actors array?

I tried this but this doesn't seems to work:
for body,sprite in pairs(actors) do
	if body ~= nil then
           .....
end
Can't you use the nil test on a body?

Thanks,

Marc


Comments

  • ar2rsawseenar2rsawseen Maintainer
    @Dikkesnoek unfortunately body may be destroyed, but it will still not be nil.
    So I'd say first you would need to check if body should be removed like this:
    if (sprite.removeMyBody) then
    	world:destroyBody(body)
    	sprite.removeMyBody = false
    end
    and only then check if sprite should be removed:
    if (sprite.removeMe) then
    	stage:removeChild(sprite)
    	actors[body] = nil 
    end
    So if you would want to destroy both body and sprite, you would have to set up both
    sprite.removeMyBody = true
    sprite.removeMe = true
  • DikkesnoekDikkesnoek Member
    edited April 2013
    Ok, thanks again my friend. I will implement it directly. Am I right
    that body is an object so I can test if it is nil? I can also add an
    property, like sprite.body.removed to tell that a body is removed.

    Regards,

    Marc
  • ar2rsawseenar2rsawseen Maintainer
    I think that body.removed should work best :)
    I'm usually doing something similar for joints
Sign In or Register to comment.