0

好的,我正在编写一个函数,它允许许多不同类型的子弹与许多不同类型的对象交互:

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);
      }
   }
}

我试图根据两个子弹类中的任何一个中的“power”变量(无论哪个被击中)来减少敌人对象的健康,但我无法弄清楚为什么它在我时不断向我抛出以下错误使用上面提到的问题代码:

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

它似乎肯定知道我正在尝试访问单个类的变量,那么为什么它不读取变量呢?

4

1 回答 1

1

我看到您想通过类实例访问静态变量。静态变量是类变量。例如:如果你的类有一个静态属性power,你可以通过这种方式访问​​它SomeButtonClass.power

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

于 2012-08-23T10:10:26.827 回答