Astraport
Astraport

Reputation: 1257

Starling and AS3 physics engine - how to organize animation

In my game a objects (bodies) move each frame and it does not matter I use Box2D or Nape.

    //for example
var body:Body = createNewBall();
addChild( body.graphic );
    addEventListener( Event.ENTER_FRAME, loop);

    private function loop():void {
    space.step(1/30, 10, 10);
    }

But Starling use animation with jugglers. Perhaps it somehow affects performance.

var body:Body = createNewBall();
addChild( body.graphic );
var tween:Tween = new Tween(body.graphic, 2);
tween.animate("x", glX);
tween.animate("y", glY);
Starling.juggler.add(tween);

But how to use the jugglers in case enter_frame animation?

Upvotes: 0

Views: 934

Answers (1)

danii
danii

Reputation: 5693

When using a physics engine usually you won't animate your objects by regular Tween added to the starling juggler, but instead you just update the position of the graphic object according to the positon calculated by the engine on each step. You are going to need to use an ENTER_FRAME so you can make the engine advance each step. Using your posted code, you could do something like:

  //for example
  var body:Body = createNewBall();
  addChild( body.graphic );

  //callback to update graphic position
  body.graphicUpdate = updateBallGraphics;

  addEventListener( Event.ENTER_FRAME, loop);

  private function loop():void
  {
    space.step(1/30, 10, 10);
  }

  private function updateBallGraphics(b:Body):void
  {
    b.graphic.x = b.position.x;
    b.graphic.y = b.position.y;
  }

There is a great tutorial on gotoandlearn.com about using Starling and Nape that covers this, and all the basics you'll need to get started. Hope this helps!

Upvotes: 2

Related Questions