Search code examples
actionscript-3variablessubclasssuperclass

AS3 Superclass variables and Subclasses error


I am creating two classes which define the general structure of objects, and many subclasses which are variations of the parents. The variables have the type declaration in the superclasses, and the value declaration in the subclasses. However:

[Body_Part_Armor.as]

package  {
    import flash.display.*;
    public class Body_Part_Armor extends MovieClip
    {
        var agility_malus;
        var hp_total;
        var defense;
        var armor_extra_height;
        var armor_extra_width;  
        var hp_left;
        public function Body_Part_Armor()
        {
            hp_left = hp_total;
        }
    }
}

[Chest_Armor_1.as]

package
{
    public class Chest_Armor_1 extends Body_Part_Armor
    {
        agility_malus = 2;
        hp_total = 20;
        defense = 7;
        armor_extra_height = 10;
        armor_extra_width = 6;

        public function Chest_Armor_1()
        {
        }
    }   
}

For the compiler-debug-test Body_Part_Armor and Chest_Armor_1 are completely fine, while

[Weapon.as]

package
{
    import flash.display.*;
    public class Weapon extends MovieClip {
        var weapon_type:Number;
        var n_attacks:Number;
        var damage:Number;
        var precision:Number;
        // some other variables in here
        var is_empty_weapon:Boolean;
        public function Weapon()
        {   
        }           
    }
}

[Empty_Arm_Weapon.as]

package
{
    public class Empty_Arm_Weapon extends Weapon
    {
        is_empty_weapon = true;
        public function Empty_Arm_Weapon() {
        }
    }   
}

give me the following error: 1120: access of undefined property is_empty_weapon (yet if I write var is_empty_weapon = true; in Empty_Arm_Weapon it will complain about conflict with inherited definition of the variable in the namespace public).

If I define the variable value is_empty_weapon = true in the constructor Empty_Arm_Weapon(), however, i won't get any error.

I guess I'm missing something really obvious, but I can't tell the difference between the two cases, yet one works and the other doesn't. Can you help me? Thanks!


Solution

  • you should move the assignment of the variable into the constructor like:

    package
    {
        public class Empty_Arm_Weapon extends Weapon
        {
            public function Empty_Arm_Weapon()
            {
                is_empty_weapon = true;
            }
        }   
    }
    

    Alternatively you could declare it with a default value of true if you want this for all inheriting classes

    package
    {
        import flash.display.*;
        public class Weapon extends MovieClip {
            protected var weapon_type:Number;
            protected var n_attacks:Number;
            protected var damage:Number;
            protected var precision:Number;
            // some other variables in here
            protected var is_empty_weapon:Boolean=true;
            public function Weapon()
            {   
            }           
        }
    }
    

    Also be sure to scope your variables so they can be accessed by sub-classes if that's what you want, mark them protected (accessible by this and sub-classes) or public (accessible to this, sub-classes and instances using this object).

    EDIT

    I believe your specific issue you see in the discrepancy between the behavior may have to do with the fact that you have a typed variable in the second case and in the first case the types are not declared. This means they would default to undefined instead of false or null or 0 which may create some strange behavior, either way this isn't from all I've learned the right way to go, setup defaults at definition or in sub-classes override defaults in the constructor.

    http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f9d.html