getChildByName for actor child class

Hay everyone,

i have this code made up
with the help of http://www.box2d.org/forum/viewtopic.php?f=8&t=2913&hilit=actor and http://www.emanueleferonato.com/2008/10/26/platform-game-basics-using-box2d/

i have main document class ware i load every thing on stage als the body movent and updating is done in the actor class and the body build per class that extends actor expmale HeroActor.

what i whant is that the actor class can get its child by name?

is this even posible or ist the only way to do this bij the b2ContactListener?



heroActor code
Code:
public class HeroActor extends Actor /// child from actor class
   {
      
      public static const HERO:String = "HERO";
      
      private var _bounds:Array;
      private var _yPos:int;
      private var _direction:int;
      
      
      public function HeroActor(name:String, parent:DisplayObjectContainer, location:Point, leftBounds:int, rightBounds:int, yPos:int)
      {
         this.name = name;
         _bounds = [ ];
         _yPos = yPos;
         _direction = 1;
      
         ///create the hero graphic
         var heroMovie:MovieClip = new HeroMovie();
         parent.addChild(heroMovie);
         
         //create the hero shapedef
         var heroShapeDef:b2PolygonDef = new b2PolygonDef();
         heroShapeDef.SetAsBox(15 / PhysiVals.RATIO, 20 / PhysiVals.RATIO);
         heroShapeDef.density = 1.0;
         heroShapeDef.friction = 0.3;
         heroShapeDef.restitution = 0.2;
         
                  
         ////create the bodydef (specify location here)
         var heroBodyDef:b2BodyDef = new b2BodyDef();
         heroBodyDef.position.Set(location.x / PhysiVals.RATIO, location.y / PhysiVals.RATIO);
         //trace (heroBodyDef.position);
         
         heroBodyDef.isBullet = true;//This tells flash to treat all boxes like bullets. This is CPU consuming with a lot of boxes.
                              //But the hit test is WAY better when you use this.
         
         heroBodyDef.userData = HeroActor.HERO;
         heroBodyDef.userData.name = "HERO";
          //add one more thing to this function
         
         
         ///create the body
         var heroBody:b2Body = PhysiVals.world.CreateBody(heroBodyDef);
         
         ///create the shape
         heroBody.CreateShape(heroShapeDef);
         heroBody.SetMassFromShapes();
         addChild(heroBodyDef.userData);
         
         super(heroBody, heroMovie, name);
         
         
         
      }
      
      
      override protected function childSpecificUpdating():void
      {
         if (_graphic.y > _graphic.stage.stageHeight)
         {
            trace ('OMG!!!! i fell')
         }
         
         super.childSpecificUpdating();
      }
      
   }
   
}


Actor code

Code:
public class Actor extends EventDispatcher
   {
      protected var _body:b2Body;
      protected var _graphic:DisplayObject;
      
      public var Left:Boolean = false;
      public var Right:Boolean = false;
      public var Up:Boolean = false;
      public var Down:Boolean = false;
      
      
      public function Actor(name:String, myBody:b2Body, myGraphic:DisplayObject)
      {
         
         _body = myBody;
         _body.SetUserData(this);
         _graphic = myGraphic;
         _body.GetUserData;
         updateMyLook();
         
         

         
      }
      
      protected function childSpecificUpdating():void
      {
         /// this funtion does nothing
         /// to be call by the childeren
      }
      
      public function destroy():void
      {
         ///remove actor from the world
         // TODO: finsh this later
         
      }
      
      
      public function updateNow():void
      {   
         //save cpu if not all body are updated
         //wil geve a problem when static object are move scprite weis
         if (!_body.IsStatic())
         {
         
         updateMyLook();
         childSpecificUpdating();
         }
      }
      public function GetBodyAtPoint(px:Number, py:Number, includeStatic:Boolean = false) {
         // Make a small box.
         var px2 = px/PhysiVals.RATIO;
         var py2 = py/PhysiVals.RATIO;
         var PointVec:b2Vec2 = new b2Vec2();
         PointVec.Set(px2, py2);
         var aabb:b2AABB = new b2AABB();
         aabb.lowerBound.Set(px2 - 0.001, py2 - 0.001);
         aabb.upperBound.Set(px2 + 0.001, py2 + 0.001);

         // Query the world for overlapping shapes.
         var k_maxCount:int = 10;
         var shapes:Array = new Array();
         var count:int = PhysiVals.world.Query(aabb, shapes, k_maxCount);
         var body:b2Body = null;
         for (var i:int = 0; i < count; ++i) {
            if (shapes[i].m_body.IsStatic() == false || includeStatic) {
               var tShape:b2Shape = shapes[i] as b2Shape;
               var inside:Boolean = tShape.TestPoint(tShape.m_body.GetXForm(), PointVec);
               if (inside) {
                  body = tShape.m_body;
                  break;
               }
            }
         }
         return body;
         
      }
      
      private function updateMyLook():void
      {
         /*Following code down to line 252 Makes the Player move left, right and jump*/
         
         var TheHero = GetChildByName("HERO");//Getts a child named HERO
         
         
         var Body = TheHero.body;//Get the var body from the child.
         
         

         if (Left) {//If Left Arrow is down
            Body.WakeUp();//Wakes the body up if its sleeping
            Body.m_linearVelocity.x = -3;//Adds to the linearVelocity of the box.
         }
         if (Right) {//If right arrow is down
            Body.WakeUp();//Wakes the body up if its sleeping
            Body.m_linearVelocity.x = 3;//Adds to the linearVelocity of the box.
         }
         if (Up) {
            if (Body.GetLinearVelocity().y > -1) {//Stops player from sometimes jumpping higher then suppose to
               /*
                              I have made 3 hit points.
                              */
               var Hit = GetBodyAtPoint(TheHero.x, TheHero.y+(TheHero.height/2+2), true);//Under
               var Hit1 = GetBodyAtPoint(TheHero.x-(TheHero.width/2)+2, TheHero.y+(TheHero.height/2+2), true);//Down-Left
               var Hit2 = GetBodyAtPoint(TheHero.x+(TheHero.width/2)-2, TheHero.y+(TheHero.height/2+2), true);//Down-Right
               if (Hit != null && Hit.m_userData != TheHero) {
                  Body.ApplyImpulse(new b2Vec2(0.0, -3.0), Body.GetWorldCenter());//Applys and impuls to the player. (Makes it jump)
               } else if (Hit1 != null && Hit1.m_userData != TheHero || Hit2 != null && Hit2.m_userData != TheHero) {

                  Body.ApplyImpulse(new b2Vec2(0.0, -3.0), Body.GetWorldCenter());
               }//Applys and impuls to the player. (Makes it jump)
            }
         }
         //update the graphic to box2d body
         _graphic.x = _body.GetPosition().x * PhysiVals.RATIO;
         _graphic.y = _body.GetPosition().y * PhysiVals.RATIO;
         _graphic.rotation = _body.GetAngle() * 180 / Math.PI;
      }
      
   }
   
}
So you want to get a child of your _graphic variable? Since it is a DisplayObject, you need to cast it to DisplayObjectContainer or MovieClip/Sprite. If the child has an instance name, you can just do something like this:

DisplayObjectContainer(_graphic).yourChildInstanceName

to get a reference to a particular child. Alternately, you can use getChildByName, but you must cast to DisplayObjectContainer, etc... first.

You can also loop through all the children like this:

var container:DisplayObjectContainer = _graphic as DisplayObjectContainer;
var i:int, len:int, child:DisplayObject;
len = container.numChildren;
for (i=0; i < len; i++) {
child = container.getChildAt(i);
//do something with each child here
}

Original thread: https://box2d.org/forum/viewtopic.php?t=3061