AS3: possible to dynamically attach things without linkage id? - actionscript-3

Probably a stupid question but..
Is it possible to dynamically add things to the stage using code WITHOUT having manually checked "export for actionscript" for each item, and without having given linkage id's?
I make pages upon pages of "drag and drop" items for people to drag around.. and it just gets unfathomably time consuming to first manually give each item an instance name, then "view in library" for each item one at a time, then modding its Properties, then checking off the box and giving each a linkageID, keeping track of the number etc. Seems like there must be a better/faster way?
In my case, the items are always on the stage to begin with, so I know that they are being exported in the swf, even without having checked off the box...
Oh hey.. since I'm starting with the item in question when the user clicks it, could they instead "duplicate" the item they clicked on? I think this was possible in AS2? Would that still require a linkage id?

Yes, you can just look through the display chain if you know that an item is already on the stage. What you get back will be a DisplayObject and you'll have to decide if it's a MovieClip or a Sprite but it can be done. You can name movie clips on the stage and find them by name, too - these don't need to be exported for scripting.
All DisplayObject instances have a name property and you can use getChildByName to find a named instance at run-time.
You only need to export a symbol for scripting if you want to control the base type of the symbol (so you can add extra properties / methods to it) or if you want to programmatically create new instances of a symbol during run-time. Anything that's on the stage (or inside a display object container on the Stage) can be accessed through an index (not recommended) or through a name and don't need to be exported.
You cannot programmatically create instances of display objects without exporting them first.

Related

Bring movieclips that have been added through editor to front

I have a game in which I have three different scenes. In the first scene, the objects (movieclips) were added through the flash editor, meaning that no actionscript was used to add them (not added with addChild). After that ive decided that I want to do the second and third scene entirely through actionscript and every object that is added is added with addChild(). The problem with this is that now the objects that have been added through actionscript appear in front of the ones added manually through the editor and I want it to be the other way around. I know that addChildAt() exists but I have over a 100 objects so it doesn't seem like a good option. In short: How do I set the Z-Indexes of movieclips that have been added through the flash editor and not through actionscript.
At first I tell you, this is not a good practice.
I give you some methods:
1.add child at to the back of others:
addChildAt(myDisplayObject,0);
so your added children will go back.
2.set the index of display obects:
first you should give them instance names, then in code:
setChildIndex(myDisplayObect,myIndex);
so you set the index of your display object.
I  H☺ P E  this helps

as3 how can i prevent that a new instance is created by entering a frame?

i am working with several nested movieclip objects in a project. but i get into trouble with the buttons i created and implemented in the nested movieclips:
to describe it in a simple way:
I have a main movieclip with five frames, including two buttons with listeners to browse between the frames. Then inside of one Frame I have another movieclip with its own buttons. i instanciated it by hand not through code and gave it a specific name like "nestedMc".
Now I dont want to build the Listeners for those buttons inside the class of the nested movieclip class but in its parent class, which works fine until i then goto another frame in the main movieclips timeline and come back.
obviously every time flash enters a frame its contents get created anew (and therefore get new instance names). I could now try solve this through filling the frames via code.
But maybe there is another way to make sure the frame contains the same instance everytime i enter?
Timeline scripting is a dirty business, and really, a carry-over compatibility layer for Actionscript 2 projects. Whenever possible, I highly recommend not doing it, and simply keeping all of your code in your document class. As you're experiencing, timeline code causes headaches.
Consider instead just creating both states of your Stage (it sounds like that's what your two buttons are jumping between) and simply hiding them offstage or setting their alpha to zero and their mouseEnabled state to false. Furthermore, if the purpose of your frames is to play animation (a tween), consider instead switching to a much more powerful suite such as TweenLite. Moving an object over a hundred pixels (smoothly) can be as easy as:
TweenLite.to(redBall, 3, {x:100});
Now, if you're manually adding these items to the stage, as long as the object is a dynamic one, you can assign an instance name to it which will be saved between frame loads. Be aware the object name is not the same as the instanced name. For example:
var redBall:Ball = new Ball();
redBall.name = "bubbles";
The object's name is Ball, but it's represented as a variable called redBall. Its actual DisplayList name will likely be ambiguous (such as "Instance71"), and I can manually define it as "bubbles". 3 different names for the same object, all very different and necessary.
Even if you give the object a displayList name, you may not be able to reference it through code unless you enable Automatically declare stage instances, which basically creates on each object a pointer to the displayList object.
That said, you can always fetch the object by other means. Obviously, your buttons are always appearing, but you're trying to find a very specific object on the stage. At this point, we can use getChildByName() or getChildAt().
Hope that helps.
-Cheers

Can two buttons with the same class instance act differently in Flash?

Say I have this specific button class in Flash called cont_button and it's supposed to be used to break out of a loop, but I want to use the class more than once. Is there a way to give every instance of this class some kind of parameter so that it knows which frame it nees to go to?
Example:
I have an instance of cont_button on frame 200 and there's a loop between 200 and 210. This cont_button executes a gotoAndPlay(211). But later on I have another instance of the button on frame 315 and a loop between 315 and 325. Is there a way to make it so each instance knows which frame it specifically needs to go to via the use of a variable? Or am I going to have to make an actionscript file for each individual one?
Pretty new to ActionScript so I appreciate the help and if there are good coding references to AS3 you guys recommend, I'll gladly look those over.
Sure, this is possible. One way you can do this is make the frame numbers class variables and when the button is clicked, they reference whatever value is stored in them, rather then hardcoded numbers. To get a better idea, can you post the relevant parts of your button class?
As you say, you need to pass a parameter to each instance of the button. There are lots of different ways you could do this, but I'd be tempted to just do it via the instance names.
You could name each button loopBreakTo211, loopBreakTo326 and so on, then in your button's class have:
var breakFrame:Number = Number(name.replace("loopBreakTo", ""));
(parent as MovieClip).gotoAndPlay(breakFrame);
Admittedly that's not a very robust way of doing it (for example, it will break if a button is named incorrectly and breakFrame ends up as NaN, so you might want to add a check for that), but it keeps the parameter together with the instance instead of in the timeline somewhere.

Correct use of addChild

I'm new to coding and AS3. I was reading about adding things to the stage using AS3 and learned about the addChild method. Reading more I found that there are different ways to use it. I also read that some ways are better than others, and that some ways are not good at all and better avoided.
I don't trust those sources though. As a coding newbie I come asking for help, StackOverflow. I'm about to start an addChild heavy project and I want to start it with the right implementations. I trust you, so let me ask you this: What's the correct use of addChild?
I just want to add things to the stage, but I read that it's not good to add them directly to the stage (without further argument though).
stage.addChild()
this.addChild()
addChild()
Are there other ways? Which one should I use?
Thanks for your time. :)
As the top-level container for all display objects in the display list hierarchy, there is only one Stage no matter how many SWF files are loaded into the runtime. So, generally, objects should not be added to the Stage, directly, at all. The only object the Stage should contain is the root object.
Generally, you should not use: stage.addChild()
Adding a DisplayObject to the display list should be performed within the scope of a DisplayObjectContainer.
Each SWF file has an associated ActionScript class, known as the main class of the SWF file which extends a display object. From this class or any child within the hierarchy you may call addChild().
The following are equal, and would add a child within the scope of the current display object container.
this.addChild()
addChild()
The this keyword explicity defines scope; however, is generally implicit when left off.
While a display object added via addChild() is added to the front (top) of all other children, to add a child to a specific index position, use the addChildAt() method.
References:
Display Programming
Basics of display programming
Adding display objects to the display list

When do we need to add reference to the stage from one class to another

When do we need to pass reference to the stage from one as3 class to another like in this tutorial
http://asgamer.com/2009/as3-flash-games-for-beginners-firing-weapons-with-delays
he added a ref to the stage from the bullet class to the ship class
as I understand a reference is needed when we want to use a function in a certain class from another class but why do we have to reference the stage isn't it only one stage for the whole project or each class has it's own stage ?
I am very confused
Thanks
Only objects that are connected to a stage will be shown on screen. In the tutorial he is adding the lasers onto the stage display list so that it appears on screen. Until it is added, it will not be rendered regardless of the visible property.
See this for more info about the display list.
#Geotarget is correct, but the answer is a little bit indirect.
Objects that are not on the display list do not actually have a reference to stage. So if, for example, you create a var mc:MovieClip = new MovieClip(); which is not added to the display list (as in addChild(mc)), then mc.stage will be null. (Also, checking if(mc.stage){[...]} is also a way to verify if the MovieClip is part of the display list yet.)
So you can pass a reference to the stage to non-display-list objects to allow them access to things like stageWidth.
Yes, a reference is needed when you wanna have to access a function present in that particular class ( to which reference belongs). This is one of the uses.
There is only one stage for the whole project.
In the tutorial you are following, both the classes are using the reference of a COMMON Stage, so that both of them can access the Stage.
It's like giving the address of a place to two people. So that both of them can go there. Naturally, giving addresses doesnot mean, we are building two places for each of them.
V.