Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
world.getChildByName() — Gideros Forum

world.getChildByName()

totebototebo Member
edited October 2013 in General questions
Hi guys, my first post!

I've started playing with Gideros and so far it's been plain sailing. That was until the point I needed to reference an Box2D body by its name. It seems Box2D has a world.getChildByName() method, which I can't seem to find a reference to in the Gideros Physics API.

Is this a missing feature, or is there another way I can reference an arbitrary body based on its name?

To give a bit of context, I have two bodies, one which contains a sensor fixture, the other a kinematic fixture. When the game character touches the sensor fixture I want to:

- Remove the sensor fixture (done)
- Add velocity to the kinematic fixture

Since the kinematic fixture is not actually part of a collision, I need another way of referencing it. Hope this makes sense.

Nic.
My Gideros games: www.totebo.com

Comments

  • ar2rsawseenar2rsawseen Maintainer
    edited October 2013 Accepted Answer
    Hello @totebo ;)
    I see,
    usually I either create a class for an object, store them as class properties and add class instance reference to bodies (or fixtures)

    as
    MyPhysicsObject = Core.class()
     
    function MyPhysicsObject:init()
        self.body1 = world:createBody{type = b2.STATIC_BODY}
        self.body1.object = self
        self.body2 = world:createBody{type = b2.STATIC_BODY}
        self.body1.object = self
    end
     
    function MyPhysicsObject:getBody2()
        return self.body2
    end
     
    --and use it like
     
    function scene:onBeginContact(e)    
        --getting contact bodies
        local fixtureA = e.fixtureA
        local fixtureB = e.fixtureB
        local bodyA = fixtureA:getBody()
        local bodyB = fixtureB:getBody()
     
        body2 = bodyA.object:ģetBody2()
    end
    But probably can be done even easier, like
    local body1 = world:createBody{type = b2.STATIC_BODY}
    local body2 = world:createBody{type = b2.STATIC_BODY}
    body1.body2 = body2
     
    --and use it like
     
    function scene:onBeginContact(e)    
        --getting contact bodies
        local fixtureA = e.fixtureA
        local fixtureB = e.fixtureB
        local bodyA = fixtureA:getBody()
        local bodyB = fixtureB:getBody()
     
        body2 = bodyA.body2
    end
    But I have not heard about world.getChildByName(), must be something new then, or some framework specific :)
  • Hi ar2, I've read many of your (very helpful) posts before, but hadn't experienced the extreme speed at which you operate!

    Thanks for the response, that makes perfect sense, and feels a bit cleaner since you'd be able to call the second object immediately without any searching.

    The thread I found mentioning .getChildByName() was on the Box2D forum where they remove a second object with this code (I'm not allowed to post links, but a google search should get you there):
    Util.remove(world.getChildByName('instance_name_of_object'));
    On a separate note, is there a legend for formatting codes to post to this forum?

    Nic.

    Likes: ar2rsawseen

    My Gideros games: www.totebo.com
    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.