Hi!
I've created a DYNAMIC_BODY as Follow:
function MyScene:createMyDynamicBody(x, y)
local DB = Bitmap.new(Texture.new("images/DB.png"))
DB:setAnchorPoint(0.5, 0.5)
DB:setPosition(x,y)
local radius = DB:getWidth()/2
local body = self.world:createBody{type = b2.DYNAMIC_BODY, fixedRotation = true}
body:setPosition(DB:getX(), DB:getY())
body:setAngle(DB:getRotation() * math.pi/180)
local circle = b2.CircleShape.new(0, 0, radius)
local fixture = body:createFixture{shape = circle, density = 10, friction = 1.0, restitution = 0.4}
fixture:setFilterData({categoryBits = 1, maskBits = 26})
DB.body = body
DB.body.type = "DB"
self:addChild(DB)
return DB
end |
And I've created a STATIC_BODY as Follow:
function MyScene:createExtraLife(x, y)
local SB = Bitmap.new(Texture.new("images/life.png"))
SB:setAnchorPoint(0.5, 0.5)
SB:setPosition(x,y)
local radius = SB:getWidth()/2
local body = self.world:createBody{type = b2.STATIC_BODY}
body:setPosition(SB:getX(), SB:getY())
body:setAngle(SB:getRotation() * math.pi/180)
local circle = b2.CircleShape.new(0, 0, radius)
local fixture = body:createFixture{shape = circle, density = 10, friction = 1.0, restitution = 0.4, isSensor = true}
fixture:setFilterData({categoryBits = 8, maskBits = 17})
SB.body = body
SB.body.type = "SB"
self:addChild(SB)
return SB
end |
And onBeingContact Function is as Follow:
function MyScene:onBeginContact(e)
--getting contact bodies
local fixtureA = e.fixtureA
local fixtureB = e.fixtureB
local bodyA = fixtureA:getBody()
local bodyB = fixtureB:getBody()
if bodyA.type and bodyB.type then
if bodyA.type == "DB" and bodyB.type == "SB" then
local extraLife
for i = 1, #self.extraLife do
extraLife = self.extraLife[i]
end
self:removeChild(extraLife)
extraLife = nil
elseif bodyB.type == "pumpkin" and bodyA.type == "exLife" then
local extraLife
for i = 1, #self.extraLife do
extraLife = self.extraLife[i]
end
self:removeChild(extraLife)
extraLife = nil
end
end
end |
And my init() Function is as Follow:
function MyScene:init()
self.DB = self:createMyDynamicBody(200, 300)
self.extraLife = {}
self.extraLife[#self.extraLife+1] = self:createExtraLife(550, 185)
self.extraLife[#self.extraLife+1] = self:createExtraLife(70, 515)
self.extraLife[#self.extraLife+1] = self:createExtraLife(200, 400)
self.extraLife[#self.extraLife+1] = self:createExtraLife(380, 380)
self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
self.world:addEventListener(Event.BEGIN_CONTACT, self.onBeginContact, self)
end |
My Question and Problem is that:
First time when I move Dynamic body in contact with the static body it works fine, The static body gets disappear.
But when I move the Dynamic body again to the position where the static body was, that time it again goes inside onBeingContact() as I've removed the static body on the first contact itself, and it throws error saying:
The supplied Sprite must be a child of the caller.
stack traceback:<\pre>
I want to create many static bodies(eg: many coins or stars or life's etc..) and each time when the dynamic body touches any static body that time only that static body should disappear from the stage permanently and when the dynamic body moves again to the same position where the static body was, it should not go inside onBeingContact()
Can any one help me out for this problem
Or any best method to achieve such action
Thank you.. <img class="emoji" src="http://forum.gideros.rocks/resources/emoji/smile.png" title=":)" alt=":)" height="20" /> |
Comments
SB.body.object = SB
I tried the above code but I'm getting error
Its working now....