0
votes

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

1
Apparently Scene has several frames, and there are frames with no thing_mc on it, so whenever moveThing() runs on one of those frames, you get the 1120. This is a known restriction of keyframes and contents, when you call gotoAndStop() to a different frame, the previous set of contents is plain destroyed. You can remove thing_mc from Scene in the GUI, then declare it as a class property in scene.as, then initialize and manage it separately.Vesper
How is this different from your other question?null
I'm sorry if I'm not following the right protocol with asking questions. It's different because someone edited my title on the other question, and I'm NOT wondering "how to add MovieClips from the library to a nested MovieClip?" I know how to do that.. since apparently my code in the other question distracted from the actual issue I was just trying to start fresh. In the future I will just edit the question if something like this happens, but for now I would love to just keep this question here since Vesper is the first person to address my actual question.staticwave
Vesper, Scene has only one frame since I was trying to keep it as simple as possible. Likewise, thing_mc only has one frame, and so does the main timeline. And you're right that I can initialize thing as a variable in scene.as, but in a real project it's just way more convenient to use the stage to place objects. If I were to add thing to the stage with .addChild(), I would have to guess and check dozens of times to get it in exactly the right place. Is the conclusion just that what I'm trying to do is not possible in AS3?staticwave
I edited your other question, because you asked about problems when adding MovieClips to the stage. If you are not happy with an edit, you can always roll it back.null

1 Answers

0
votes

...From what I understood after an all nighter (and sorry if this isn't the answer you're looking for)

You need to link Scene (the object in your library in Adobe Flash (or Adobe Animate if you're using that)) to your class. (Export for Actionscript) BUT (and here's the kicker) you need to also check export for frame 1 and make sure it's loaded on frame 1.

I run into issues like this when I'm not paying attention. Make sure the movieclip nested inside your main movieclip, somehow, gets loaded. Be it through the constructor, or on frame 1. Say your nested movieclip is on frame 2. Well, flash hasn't loaded frame 2. Therefor, it hasn't constructed anything inside frame 2. So it has no idea what you're talking about.

At times, i've had to do a gotoAndStop(2) and then right back to 1 just so that an object on frame 2 was named and ready to be handled in the future.

enter image description here