2012-08-03 50 views
0

好一个子类的接取变量,我写一个函数,将允许许多不同类型的子弹与许多不同类型的对象进行交互:AS3 - 使用参数

private function checkBulletCollisions() :void{ 

    var bullet:MovieClip; 
    for (var j:int = 0; j < shootableArray.length; j++){ 
     shootableObject = shootableArray[j]; 
     for(var i:int = 0; i < bulletArray.length; i++){ 
      bullet = bulletArray[i]; 
      if (shootableObject.hitTestPoint(bullet.x, bullet.y, true)) { 

       container.removeChild(bullet); 
       bulletArray.splice(i,1); 

       if (shootableArray[j] is Enemy){ 

        shootableObject.enemyHealth += zArrow.power; //Working code for zArrow only 
        shootableObject.enemyHealth += bullet.power; //Error throwing code 
        //(I'm not using both lines at the same time, in case you were wondering) 

        if(shootableObject.enemyHealth <= 0){ 
         container.removeChild(shootableArray[j]); 
         shootableArray.splice(j,1); 
        } 
       } 
      } 
     } 
    } 

现在,我有两种类型的子弹(zArrow和Dice)都可以扩展Bullet类。这里是zArrow类:

package 
{ 
    import Bullet; 
    public class zArrow extends Bullet 
    { 
     public static var power = -1 


     public function zArrow(anything:*):void 
     { 
     super(anything); 
     } 
    } 
} 

我想基于无论是在两颗子弹类的“权力”变量(哪一个被击中),以减少敌方目标的健康,但我不能弄清楚为什么它让我扔了以下错误,当我使用上面提到的问题代码:

ReferenceError: Error #1069: Property power not found on zArrow and there is no default value. 
    at GameDocumentClass2/checkBulletCollisions() 
    at GameDocumentClass2/enterFrameHandler() 

它肯定似乎知道我试图访问一个单独的类的变量,那么,为什么它不读取变量?

回答

1

我看到你想通过类实例访问静态变量。静态变量是类变量。例如:如果你的班级有一个静态属性power,你可以这样访问SomeButtonClass.power

静态变量不会被子类继承。只有非静态的publicprotectedinternal属性被继承。

+0

我已经开始使用Static了,那又怎么样? :(我的var现在是公开的,我仍然可以从另一个类访问它吗? – 2012-08-23 13:00:39