Jump to content
  • Advertisement
Sign in to follow this  
__SKYe

AS3 Acessing Child's Vars

This topic is 2594 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
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!