• Advertisement
Sign in to follow this  

AS3 Acessing Child's Vars

This topic is 2352 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement