It looks like you're new here. If you want to get involved, click one of these buttons!
Caveman = Core.class(Sprite); -- Do on load function Caveman:init(scene,atlas) local animLoader = CTNTAnimatorLoader.new() animLoader:loadAnimations("Animations/atlas-2-animation.tan", atlas, true) local caveman = CTNTAnimator.new(animLoader) caveman:setAnimation("CAVEMAN_FALLING") caveman:addToParent(caveman) caveman:playAnimation() local body = scene.world:createBody{type = b2.DYNAMIC_BODY} body:setPosition(caveman:getX(), caveman:getY()); body:setAngle(caveman:getRotation() * math.pi/180); local poly = b2.PolygonShape.new() poly:setAsBox((caveman:getWidth()/2)-10,10,0,4,0) local fixture = body:createFixture{shape = poly, density = 1.0, friction = 0.1, restitution = 0.2} local filterData = {categoryBits = 2, maskBits = 1}; fixture:setFilterData(filterData); caveman.body = body self:addChild(caveman) --caveman.body.type = "caveman"; self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self) end |
Comments
By default, Gideros doesn't keep any real links between physics bodies and their associated sprites. You have to do this yourself.
Take a look at how the Sleeping Bodies example does it. You'll see that there is a global ENTER_FRAME handler that steps the physics world on by 1/60 sec. This will move the physics bodies to their new locations. However, at this point the sprites are still where they were before. What the example does is to keep another table called "actors" that has a mapping back from physics body to sprite. The ENTER_FRAME handler goes through all the bodies in the actors table and updates the position of the equivalent sprite to match.
That is one way to do it. An alternative might be to use @ar2rsawseen's new GiderosInit library. See this thread. I haven't really used this part of it but the example program seems to show an easier way to use physics where the bodies and sprites are automatically tied together.
best regards
Likes: Tom2012
Thanks for the reply ;-)
I've actually got the method that updates all the box 2D bodies each frame. The physics body falls away by itself while the sprite remains at the top of the screen.
If I take the code from the class and place it on the scene, it works fine. I've got something wrong with the child / parent I think?
if my guess is true then try this in scene.lua
Likes: Tom2012
If anyone in the future wants to use TNT animator and physics, in a class that spawns sprites, here's my code.
The scene: