vendredi 24 mars 2017

Accessing properties after using getdefinitionbyname

I'm trying to access properties from a class like this:

public class Oak extends Tree //base class for tree classes
{
    public static const CLASS_NAME:String = getQualifiedClassName(Oak);

    public var branches:Array;

    public function Oak() 
    {
        branches = 
        [
        "Leaf1",
        "Leaf2",
        ];
    }

The class trying to access the variable is shown here:

public class TreeTest extends BaseScreen //just extends Sprite, TreeTest is shown on the stage
{
    public var oak:Tree;

    private var inc:int;

    public function TreeTest(pModel:Model) 
    {
        model = pModel;

        model.createTree(Oak.CLASS_NAME);
        oak = model.getTree(Oak.CLASS_NAME);
        inc = 0;

        this.addMouseClickListener(0, treeHandler); //from BaseScreen
    }

    private function treeHandler(e:MouseEvent):void 
    {
        inc++;
        this.displayText.text = oak.branches[inc];
    }

and the model is shown below:

public class Model 
{
    public var treeArray:Array;

    public function Model() 
    {
        treeArray = new Array();
    }

    public function createTree(pClassName:String):void 
    {
        var name:String = pClassName;
        var ClassReference:Class = getDefinitionByName(name) as Class;
        var classInstance:Tree = new ClassReference;
        treeArray.push([name, classInstance]);
    }

    public function getTree(pClassName:String):Tree
    {
        var treeName:String = pCharacterClassName;
        var match:Boolean = false;
        var matchArrayRef:int = 0;

        for (var i:int = 0; i < treeArray.length; i++) 
        {
            if (match == true) 
            {
                break
            }

            if (treeArray[i][0] == treeName) 
            {
                match = true;
                matchArrayRef = i;
            }
            else 
            {
                match = false;
            }
        }

        if (match == false) 
        {
            return null; 
        }
        else 
        {
            return treeArray[matchArrayRef][1];
        }
    }

When I ran this, I got the error "Access of possibly undefined property branches through a reference with static type Tree".

After searching for a solution, I discovered that basically means that the branches array was not in the Tree class. To confirm this, if I went and took public var branches:Array away from the Oak class and put it in the Tree class, the code worked.

Why can I not access variables defined in the Oak class, and have to put everything in the Tree base class? Even if I change var classInstance:Tree to var classInstance:Oak, or even var classInstance:Object, I still get the same error if the array is in the Oak class, not the Tree class.

Like, if I create a new class Pine which would also extend Tree, do I have to put all of Pine's variables in the Tree class too?

Aucun commentaire:

Enregistrer un commentaire