sprite not acting as a costume on my body

Discuss issues specific to the Flash/AS3 port of Box2D
Farjuan
Posts: 17
Joined: Thu Mar 07, 2013 4:00 pm

sprite not acting as a costume on my body

Postby Farjuan » Sat Mar 09, 2013 8:27 pm

Hi I am using box2d and I have a circle shape, I am trying to get a sprite to act as a costume to mask the body, but the sprite doesent move, only inserted in the world. I have an actor class and a ballActor that extends actor.

Actor:

Code: Select all

      public function Actor(myBody:b2Body, myCostume:DisplayObject)
      {
         _body = myBody;
         _body.SetUserData(this);
         _costume = myCostume;
         
         updateMyLook();
         
      }


...

Code: Select all

private function updateMyLook():void
      {
         _costume.x = _body.GetPosition().x * PhysiVals.RATIO;
         _costume.y = _body.GetPosition().y * PhysiVals.RATIO;
         _costume.rotation = _body.GetAngle() * (180 / Math.PI);
      }


ballActor:

Code: Select all

public var fixtureDef:b2FixtureDef = new b2FixtureDef();
      
      public function ballActor(parent:DisplayObjectContainer ,location:Point)
      {
         
         var fred:Sprite = new Fred();
         fred.scaleX = BALL_SIZE / fred.width;
         fred.scaleY = BALL_SIZE / fred.height;
         parent.addChild(fred);
         
         var wheelShape:b2CircleShape = new b2CircleShape(BALL_SIZE / PhysiVals.RATIO);
         
         fixtureDef.shape = wheelShape;
         fixtureDef.density=1;
            fixtureDef.friction=5;
            fixtureDef.density = 0.1;
         
          var wheel:b2Body;
         var BodyDef:b2BodyDef = new b2BodyDef();
         
         BodyDef.type=b2Body.b2_dynamicBody;
         BodyDef.allowSleep = false;
         BodyDef.position.Set(location.x / PhysiVals.RATIO, location.y / PhysiVals.RATIO);
         wheel = PhysiVals.world.CreateBody(BodyDef);
         wheel.CreateFixture(fixtureDef);
         
         
         super(wheel, fred);
         
      }


the problem that happens is when I add it to main class and add it to an array so I can call updateLook() in the new frameevent listenr:

Code: Select all

var ball:ballActor = new ballActor(_camera, new Point(240 / PhysiVals.RATIO, 20 / PhysiVals.RATIO));
         _allActors.push(ball);

the sprite and body are only added at the top left corner, not the point i ask it and more importantly **the sprite doesent move**, what am I doing wrong? Is there a more proper way to update the sprite location?

jayther
Posts: 266
Joined: Sun Oct 25, 2009 3:28 am
Contact:

Re: sprite not acting as a costume on my body

Postby jayther » Sun Mar 10, 2013 9:58 pm

It's because you divided the location coordinates by PhysiVals.RATIO twice. When you make the new ballActor, you passed in a Point object as a location with coordinates that are divided by the ratio, and in the ballActor constructor, you divided by the ratio again.

Farjuan
Posts: 17
Joined: Thu Mar 07, 2013 4:00 pm

Re: sprite not acting as a costume on my body

Postby Farjuan » Tue Mar 12, 2013 5:14 pm

jayther wrote:It's because you divided the location coordinates by PhysiVals.RATIO twice. When you make the new ballActor, you passed in a Point object as a location with coordinates that are divided by the ratio, and in the ballActor constructor, you divided by the ratio again.


thank you again for the help! This solves one problem, but now I have to solve the big actual problem, the costume sprite doesent follow the ballActor, it just stays there,

Code: Select all

this is the code in the newFrameEvent:

      private function newFrameEvent(e:Event):void
      {
          PhysiVals.world.Step(1.0 / 20.0, 10, 10);
         PhysiVals.world.ClearForces();
         PhysiVals.world.DrawDebugData();
         for each (var actor:Actor in _allActors) {
            actor.updateNow();
            trace("is this actually working?");
         };
      }


the trace statment works fine but the updateNow() method doesent seem to do anything, this is the code for that method in the actor class which ballActor extends:

Code: Select all

private function updateMyLook():void
      {
         _costume.x = _body.GetPosition().x * PhysiVals.RATIO;
         _costume.y = _body.GetPosition().y * PhysiVals.RATIO;
         _costume.rotation = _body.GetAngle() * (180 / Math.PI);
      }


thank you once again, I really appreciate your help!

jayther
Posts: 266
Joined: Sun Oct 25, 2009 3:28 am
Contact:

Re: sprite not acting as a costume on my body

Postby jayther » Tue Mar 12, 2013 5:59 pm

Sorry if this is too obvious, but does actor.updateNow() actually call the actor's updateMyLook() function?

Farjuan
Posts: 17
Joined: Thu Mar 07, 2013 4:00 pm

Re: sprite not acting as a costume on my body

Postby Farjuan » Tue Mar 12, 2013 7:09 pm

jayther wrote:Sorry if this is too obvious, but does actor.updateNow() actually call the actor's updateMyLook() function?


ah I completely forgot, I was just using things from other tutorials and update now is:

Code: Select all

      public function updateNow():void
      {
         if(!_body.IsActive()) {
         updateMyLook();
         }
         childSpecificUpdating();
         
      }


it turns out I was using a tutorial that used an older version and it used to be:

Code: Select all

      public function updateNow():void
      {
         if(! _body.IsStatic()) {
         updateMyLook();
         }
         childSpecificUpdating();
         
         
      }


sorry i forgot that crucial detail and thanks again

jayther
Posts: 266
Joined: Sun Oct 25, 2009 3:28 am
Contact:

Re: sprite not acting as a costume on my body

Postby jayther » Tue Mar 12, 2013 10:26 pm

So it's fixed or is it not fixed?

In case it's not fixed (as just looking at that bit of code), it is supposed to be IsStatic(). IsStatic() doesn't exist in Box2D 2.1, so the equivalent would be:

Code: Select all

_body.GetType() == b2Body.b2_staticBody;


In your case, the code should look like this so it only updates the costume for bodies that are not static (i.e. dynamic and kinematic):

Code: Select all

      public function updateNow():void
      {
         if(_body.GetType() != b2Body.b2_staticBody) {
         updateMyLook();
         }
         childSpecificUpdating();
         
      }


The IsActive() function does not determine if the body is static or dynamic. Bodies can be deactivated (via SetActive(false)) so the calculations for that body isn't processed and nothing will collide with it until you set it active again.

Farjuan
Posts: 17
Joined: Thu Mar 07, 2013 4:00 pm

Re: sprite not acting as a costume on my body

Postby Farjuan » Sat Mar 23, 2013 2:08 pm

ah sorry forgot to thank you but that worked! Thanks a bunch for the help


Return to “Flash”



Who is online

Users browsing this forum: No registered users and 2 guests