I’m having an issue trying to edit the values of a variable inside a MovieClip Class instance inside a container sprite.
The movieclips are all children of the sprite “container” but when I try to cycle through them with container.getChildAt(i).variable = value; within a for loop i get the error-
Scene 1, Layer 'AS', Frame 1, Line 87 1119: Access of possibly undefined property name_ through a reference with static type flash.display:DisplayObject.
How can I edit these variables? What is going wrong?
Most likely the problem is that getChildAt() returns a DisplayObject. DisplayObject is not a dynamic class, which means you can’t arbitrarily create and access variables. MovieClip on the other hand is a dynamic class, so you can create and access arbitrary variables as you are trying to do. The problem is that even though you know that the object you are accessing is a MovieClip, Flash does not know this and restricts you to the capabilities of DisplayObject, which as mentioned before is what getChildAt() returns.
In order to get around this, you must explicitly let Flash know that the object that you are dealing with is a MovieClip. This is done through what is know as casting:
Since you’ve now let Flash know that the object will be a MovieClip, you can take advantage of the fact that MovieClip is a dynamic class and access your variables.