actionscript-3addchild

AS3 - How to solve this access of undefined property error?


I need to call a MovieClip that gets added from the library but I get this error:

1120: Access of undefined property light.

This is my code:

var btn:Btnselect;

btn = new Btnselect();
con.addChild(btn);
btn.name = "light" 

light.box.visible=false

Why is it undefined?


Solution

  • You are trying to access the movieClip like you would in the Flash IDE, where you set the name property in the properties panel and that's how you access it in AS3

    However, when instantiating movieClips in AS3 (meaning, you didn't drag them from the Library to the Stage) the variable is set differently.

    Here, you should be referencing "btn" not "light"

    btn.box.visible = false;
    

    because that's what you called your variable