I have a basic AS3 question that has to do with the relationship between object on the stage and objects that are created and controlled by AS3 script. This question has already been addressed tons of times, and I've researched it for hours, but I found drastically different answers, many which were really complicated. I'm looking for the simplest, most general solution to this issue that is humanly (or perhaps I should say computerly) possible.
I want to be able reference an instance of an object on the stage inside a class that is not the main document class. Before I try to explain in my own terms, it might be better to view one of these posts, which cover the exact topic I'm confused about. If any of you understand the issue, and understand the solutions to one of these posts, perhaps you could translate one of the correct answers into n00banese for me. Rather than just focusing on how they did it, you could explain WHY it was necessary to do it that. I would be very thankful if someone could do that much, and if you can it is not necessary to read any further into this post.
AS3 - Access MovieClip from stage within a class
How do I access a MovieClip on the stage from the Document Class?
AS3 Modify stage Objects from a class
how do I make non-document-class classes 'aware' of stage components in Flash AS3?
Accessing ActionScript3 Nested Movie Clips from Class
Since it's highly possible that all of the above-mentioned situations are too complicated for me, I recreated the problem in the simplest, most general way possible in hopes that it would be easier to explain in this case.
I have a MovieClip on the stage with instance name scene_mc. scene_mc does nothing except separate one scene from another. It's linked to a .as file named Scene. Nested inside scene_mc there is a movieclip called thing_mc, linked to .as file Thing. I want to move thing_mc across the scene. I could easily do this by changing its coordinates in the main document class, something like this:
thing_mc.x = thing_mc.x + 100;
But since thing_mc is nested inside of scene_mc, I would like to control thing_mc's position inside the SCENE class so that the coordinates are relevant to other nested symbols in this class. And because in more complicated projects, it just makes sense to control a nested symbol in the code of its parent. So inside Scene.as, I might create this method:
public function moveThing() {
thing_mc.x = thing_mc.x + 200;
}
This causes this error: "1120: Access of undefined property thing_mc."
Error 1120 happens any time one tries to reference a stage instance of a symbol a .as file that is not the main document class.
So to reiterate my question, how can I let the Scene class know what thing_mc is? Or how can I let any class know what any stage instance is? How do you reference a stage instance of an object through a non-document class?
Oh! This might be helpful. The Actioscript 3.0 docs say this- Note: Children instances placed on the Stage in the Flash authoring tool cannot be accessed by code from within the constructor of a parent instance since they have not been created at that point in code execution. Before accessing the child, the parent must instead either create the child instance by code or delay access to a callback function that listens for the child to dispatch its Event.ADDED_TO_STAGE event.
So I mean, really I get why what I'm trying to do would cause an error, but I still don't get how to work around it.
Anyone who actually read this far into my gibberish, thank you. You are a kind and patient soul, haha. This issue is really consuming me so any attempt to help me understand the theory of how this works would be IMMENSELY appreciated. :D
Scene
has several frames, and there are frames with nothing_mc
on it, so whenevermoveThing()
runs on one of those frames, you get the 1120. This is a known restriction of keyframes and contents, when you callgotoAndStop()
to a different frame, the previous set of contents is plain destroyed. You can removething_mc
fromScene
in the GUI, then declare it as a class property inscene.as
, then initialize and manage it separately. – Vesper