Joseph Wagner
Joseph Wagner

Reputation: 323

AS3 - Acess variable of a subclass using parameters

Okay, I am writing a function that will allow many different types of bullets interact with many different types of objects:

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

Right now, I have two types of bullets (the zArrow and the Dice) that both extend the Bullet class. Here is the zArrow class:

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


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

I am trying to reduce the health of the enemy object based on the "power" variable in either of the two bullet classes (whichever one is hitting), but I can't figure out why it keeps throwing the following error at me when I use the problem code mentioned above:

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

It definitely seems to know that I'm trying to access a variable of an individual class, so why is it not reading the variable?

Upvotes: 0

Views: 260

Answers (1)

Rytis Alekna
Rytis Alekna

Reputation: 1377

I see that you want to access static variable through class instance. Static variable are Class variables. For exmaple: if your class has a static property power you can access it this way SomeButtonClass.power.

Static variables are not inherited by child classes. Only non static public, protected and internal properties are inherited.

Upvotes: 1

Related Questions