Friday, February 12, 2010

Newbie tip: Declaring and Managing Linked MovieClip Instances in Parent Classes

Suppose that we have two MovieClip-based classes called MegaNode and SimpleNode. We have created both of them by drawing two vector shapes on the scene and converting them to those Movieclip-based classes. For some reason, we want to call the instance of the shape that's inside - mainShape, therefore we convert it to a MovieClip, and call the instance mainShape.

Now, since on many occasions the MovieClip class is too general for our purposes, we define a SuperNode class, which extends MC, and servers as the parent for both M
egaNode and SimpleNode. We need to be able to manipulate the mainShape instance, so we instantiate it in the SuperNode class:

public var mainShape:MovieClip;

if we simply do that, the compiler will throw the following error: "1152: A conflict exists with inherited definition SuperNode.mainShape in namespace public."
The reason for that is that by default, all instances of linked MovieClips, such as mainShape, are automatically declared by default at runtime. In order to make sure that we don't get into that error again, we need to go to the ActionScript s
ettings, and uncheck the "Automatically declare stage instances" checkbox. With that done, the above mentioned code will work, and we'll be able to manipulate the mainShape instance from the SuperNode class

NOTE: Make sure that instances like mainShape are always declared as public


No comments: