从在AS3中的movieClip内添加的子访问Parents数组

时间:2015-01-20 21:26:48

标签: arrays actionscript-3 parent children

我有一个movieClip对象(Creature),在其中,我有想要添加到Creatures Array HitPoints的生命值。我想这样做的方法是让每个可能有hitPoints的Parent都有一个名为HitPoints的数组,这样我就可以将每个点自己添加到Array中,无论父类如何。 IE,骨架生物和地精生物可以在其基本movieClip内使用相同类型的hitPoint对象,但仍然可以访问。

我已经省略了这个类的一些不必要的部分

package Assets.Creatures 
{
import Assets.Points.HitPoint;
import Assets.GameStates.Main;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.geom.Point;
import flash.utils.Timer;
import flash.events.TimerEvent;


public class BaseAI_ extends MovieClip 
{
        //Characteristics
    protected var Health : int = 10;
    protected var SPEED : int = 3;
    protected var Intellect : int = 3;
    protected var Strength : int = 3;
    protected var Dexterity : int = 3;
    protected var AttackRange : int = 70;
    protected var Temperment : String = "Agressive";
    //public var Temperment : String = "Passive";
    //public var Temperment : String = "Defensive";

        //ARRAYS
    public var HitPoints : Array = new Array;


        // AttackTimers
    protected var canAttack : Boolean = false;
    protected var AttackSpeed : int;
    protected var AttackTimer : Timer;
    protected var AbilityCooldownsUP : Boolean = false;
    //protected var ABILITY : Timer;

        //AI STATES
    protected var CURRENT_STATE : int = 0;
    protected static var STATE_IDLE : int = 0;
    protected static var STATE_ENGAGE : int = 1;
    protected static var STATE_ATTACK : int = 2;
    protected static var STATE_FLEE : int = 3;

        //Reference
    protected var Target : MovieClip;

        //Directions
    protected var SlopeY : Number;
    protected var SlopeX : Number;
    protected var EngagePosition : Point;
    protected var RunDirection : Number;

    public function BaseAI_() 
    {
        addEventListener(Event.ADDED_TO_STAGE, Awake);

    }   // constructor code

    private function Awake (e : Event) : void
    {
        AttackSpeed = (5000 / Dexterity);
        AttackTimer = new Timer (AttackSpeed, 0);
        AttackTimer.addEventListener(TimerEvent.TIMER, AttackSpeedTimer);
        AttackTimer.start();


        Main.main.EnemyArray_2.push(this);
        stage.addEventListener(Event.ENTER_FRAME, update);  
    }

    public function AddHitPoints (hitPoint : MovieClip) : void
    {
        HitPoints.push(hitPoint);
    }
}

并且hitPoint类是

package Assets.Points 
{
import Assets.Creatures.BaseAI_;
import Assets.Creatures.Player_;
import Assets.Creatures.Player_Human;
import flash.display.MovieClip;
import flash.events.Event;


public class HitPoint extends MovieClip 
{


    public function HitPoint() 
    {   // constructor code
        addEventListener(Event.ADDED_TO_STAGE, Awake);
    }   // constructor code

    private function Awake (e : Event) : void
    {
        trace("HitPoints parent is " + parent);
        parent.AddHitPoints(this);
        //parent.HitPoints.push(this)
        //trace("my parent has " + parent.HitPoints.length);
    }


}
}

所以在子类(hitPoints)中我将hitPoints直接添加到舞台上的ParentClass BaseAI_,我尝试了几种不同的方法来直接添加HitPoints与parent.AddHitPoints(this);和parent.HitPoints.push(this)

1 个答案:

答案 0 :(得分:0)

我认为你需要使用this.parent而不仅仅是父。