Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Removing sprite and body causing confusion — Gideros Forum

Removing sprite and body causing confusion

KarrizKarriz Member
edited August 2013 in General questions
Hi, I've ran into a problem when trying to remove a sprite and its body from the scene. I get different errors no matter what I try.

I'll put some code to clarify what I'm trying to do:


function gamescene:init()
Level = TiledAsWorldEditor.new("level.lua")
self:addChild(Level)

self.lander = self:lander(600, 50)

end


function gamescene:lander(x, y)


local lander1 = Bitmap.new(Texture.new("images/lander.png"))
local lander2 = Bitmap.new(Texture.new("images/lander2.png"))
local lander3 = Bitmap.new(Texture.new("images/lander3.png"))

lander1:setAnchorPoint(0.5, 0.5)
lander2:setAnchorPoint(0.5, 0.5)
lander3:setAnchorPoint(0.5, 0.5)

local lander = MovieClip.new{
{1, 1, lander1},
{2, 4, lander2},
{5, 7, lander3},
}

lander:setGotoAction(7, 2)

function lander:switchAnimation()

if stopped == true then
self:gotoAndPlay(2)

stopped = false

end

end

function lander:switchAnimation2()
self:gotoAndStop(1)

stopped = true
end

lander:setPosition(x,y)

local body = Level.B2_Worlds[1]:createBody{type = b2.DYNAMIC_BODY}
body:setPosition(lander:getX(), lander:getY())
body:setAngle(lander:getRotation() * math.pi/180)
body:setLinearVelocity(5, 0)

local polygon = b2.PolygonShape.new()
polygon:set(0, -22, 16, 10, -16, 10)
local fixture = body:createFixture{shape = polygon, density = 1.0,
friction = 0.6, restitution = 0.01}
lander.body = body
lander.body.type = "lander"

Level:addChild(lander)

return lander
end



So, I have this spaceship called "lander" that I want to destroy when something happens. I'm able to apply forces to it, move it, rotate it and do many other things just fine. But I'm not able to remove it.

Level.removeChild(lander) would probably be the most obvious solution, but it doesn't work. It gives me an error: bad argument #1 to 'removeChild' (Sprite expected, got nil)

If I try to remove the body with Level.B2_Worlds[1]:destroyBody(self.lander.body), I get the following error: classes/TiledAsWorldEditor.lua:756: Body is already destroyed.

I know that TiledAsWorldEditor isn't a official part of Gideros, but hopefully someone who is familiar with it could help me. Thanks for reading.


Edit: I think I got it. The magic words were:

self.lander:removeFromParent()

and

Level.B2_Worlds[1]:destroyBody(self.lander.body)

Likes: ricvieira

+1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.