Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Sprites, layers and physics bodies... — Gideros Forum

Sprites, layers and physics bodies...

Tom2012Tom2012 Guru
edited October 2012 in General questions
When adding a 'layer' to a scene like this:
local layer = Sprite.new();
self:addChild(layer);
How do you then get the layer child included in the onEnterFrame loop that updates all the physic bodies and sprites:
local function onEnterFrame() 
 
-- edit the step values if required. These are good defaults!
self.world:step(1/60, 8, 3)
 
for i = 1, self:getNumChildren() do
local sprite = self:getChildAt(i)
if sprite.body then
local body = sprite.body
local bodyX, bodyY = body:getPosition()
sprite:setPosition(bodyX, bodyY)
sprite:setRotation(body:getAngle() * 180 / math.pi)
end
end
end
Would you create a separate loop for each layer?

Thanks

Comments

  • Well if you have multiple layers hierarchy for Sprites representing box2d objects (or same case if you have more none box2d objects than with box2d on looping layer), it would make more sense to create a separat table with references to all bodies or sprites to loop through and do the position update.
    Or else you would simply waste your valuable resources looping through objects on multiple dimensions or through objects, that does not have box2d bodies attached.

    Look at http://www.giderosmobile.com/forum/discussion/1780/extending-gideros#Item_1 implementation of box2d wrapper for updating objects ;)
  • got it! Thanks. ;-)
    -- Function to update box2D world each frame
     
    local function onEnterFrame() 
     
    -- edit the step values if required. These are good defaults!
    self.world:step(1/60, 8, 3)
     
    for i,v in pairs(spritesOnScreen) do
    local sprite = v;
    local body = sprite.body
    if(not body.destroyed) then
    local bodyX, bodyY = body:getPosition()
    sprite:setPosition(bodyX, bodyY)
    sprite:setRotation(body:getAngle() * 180 / math.pi)
    else
    -- the body has been removed, we dont need this sprite in the table any more
    table.remove(spritesOnScreen,i)
    print("removed");
    end
    end
    end
Sign In or Register to comment.