views:

27

answers:

1

If I create an object on stage in Flash Pro CS5 and want to reference it in code, how would I do so? Let's say I have 2 movie clips which I have converted to symbols and given instance names. How would I reference one of the clips? The instance name doesn't seem to work.

A: 

It depends on where you have written the code. If you have written it in the timeline on frame1 then any object on the stage in frame1 with a unique instance name can be referenced.

This also applies to the document class. The only difference is that you should only try referencing when you know the document class has been added to the stage, thus:

addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true); //this goes in the constructor of the document class and code goes in the init function (you can all this whatever you want)

If you want to refer to stage objects in other external classes then there are two ways to my knowledge. First, is to pass parameters of the stage object, so from document class pass to an external class, which can then pass to any external classes it has (assuming there is a parameter for it). You could also just pass the stage object and then do something like stage.myInstance.anotherInstance in the external class.

Alternatively, what I like to do is create a class that just holds public static variables. I then assign the stage objects to them in the document class. Then in my external classes I can just import the static class and easily refer to the stage object. I find this reduces mess and allows refactoring.

Allan
Allan, thank you so much. You would not believe how many times I have asked this question over the past week, and no one seemed to understand what I was asking.
koliver66