AS3 Acessing Child's Vars

Started by
0 comments, last by _paf 12 years, 7 months ago
Please consider the following:

class Engine extends Sprite
{
function Engine():void
{
this.addChild(new MyObj());
}
}

class MyObj extends Sprite
{
public var myVar:uint = 197;

public function MyObj():void
{
this.name = "OBJECT_1";
}
}

And then if i try to do this in Engine.as constructor after adding child:

trace(this.getChildByName("OBJECT_1").myVar);

it prints an error saying "Acess of possibly undefined..."

What i want to know is if it's possible to acess a var of a child, without making a var for that child (like "private var obj:MyObj = new MyObj();", and then using "obj.myVar") without using the static keyword, because that will make the var "myVar" belong to the class, and not the instance, and that's not what i want.

Hope you understand what i meant, cuz it's kinda messy...

Thanks in advance
Advertisement
So sorry for wasting your time, i found the answer (should have searched a little)...

Anyway, if anyway wants to know, it's possible to do it:

(this.getChildByName("OBJECT_1") as MyObj).myVar;

but it must be typecasted

Once again sorry for the trouble

This topic is closed to new replies.

Advertisement