Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Example: DYNAMIC_BODY contact with STATIC_BODY (isSensor=true for STATIC_BODY) — Gideros Forum

Example: DYNAMIC_BODY contact with STATIC_BODY (isSensor=true for STATIC_BODY)

Sush19Sush19 Member
edited September 2013 in General questions
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

  • Simply removing child is not enough, you also need to destroy a body, or at least set it to inactive, like:
    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)
    			bodyB:setActive(false)
    			extraLife = nil
  • I tried by setting it to inactive but I'm getting error saying:
    World is locked.
    As I've many Static bodies, How do I know which Static body was in contact with a Dynamic body on onBeingContact(), Right now when I remove a static body on contact with dynamic body the firstly add Static body gets disappear, although if contact is with lastly added static body in next contact the secondly added static body gets disappear and so on..
    :( :( :(
  • ar2rsawseenar2rsawseen Maintainer
    edited September 2013
    firstly add this line to MyScene:createExtraLife
    SB.body.object = SB
    And then inside collision checking do this:
    if bodyA.type == "DB" and bodyB.type == "SB" then
    	bodyB.object:removeFromParent()
    	Timer.delayedCall(1, function()
    		bodyB:setActive(false)
    		--or remove body completely
    		self.world:destroyBody(bodyB)
    	end)
  • Hi,
    I tried the above code but I'm getting error
    testing.lua:133: bad argument #1 to 'removeChild' (Sprite expected, got no value)
    stack traceback:
    	testing.lua:143: in function <testing.lua:142>
    I've attached the example file...
    zip
    zip
    onContactTest.zip
    17K
  • hgvyas123hgvyas123 Guru
    Accepted Answer
    try this function
     
    function MyScene:onBeginContact(e)
            local fixtureA = e.fixtureA
            local fixtureB = e.fixtureB
            local bodyA = fixtureA:getBody()
            local bodyB = fixtureB:getBody()
     
     Timer.delayedCall(1,function()
    	if bodyA.type and bodyB.type then
    		if bodyB.object then
    			bodyB.object:removeFromParent()
     
    			bodyB.object = nil
    			bodyB:setActive(false)
    			--or remove body completely
    			self.world:destroyBody(bodyB)
    		end
     
     
     
    	end
    end)
    end
  • Thanks @hgvyas123
    Its working now....
    :) :) :)
Sign In or Register to comment.