How to access/hide/show Layers from ActionScript [Flash CS5] - actionscript-3

I have 2-3 layers in my movie clip and i wanto access them using ActionScript code.
I want to show hide and access Elemts(buttons, text box) in that layer.
So how can i do that using ActionScript-3
Thanks.

You can't access the IDE layers programmatically. The best approach would be to group your layer contents into MovieClips with instance names that you can then call though code.

This is how I did it,
You cannot hide layers in actionscript.
You can put what you want to hide or show as a movieclip object.
Just go to insert and insert a movieclip object.
After this, put the movieclip object on the layer, just the same way you put images onto a layer.
However you must name this instance inorder to access it.
So if its called Movie1
this is how you access it,
_root.Movie1:MovieClip;
trace(""+_root.Movie1); //Just to be sure its defined
//To hide it just do this
_root.Movie1_visible = false;
//To show it just do this
_root.Movie1_visible = true;

Related

Convert a symbol to bitmap in Flash using Action Script

I am developing a small game using Flash CC. The question may seem very absurd since I am a novice to coding and Action Script.
Here it goes: Can we write a code to convert a symbol to bitmap?
Actually, the game has multiple objects and I have defined them as buttons. When the user clicks on one of the object, it moves to a new position. I dont want two objects simultaneously to move to a new position.
My logic: If I can make every other object as a bitmap, the user wont be able to click on any other object when one object is moving. Any thoughts???
Yes that is possible.
this code makes a bitmap from your displayObject:
var bitmapData:BitmapData=new BitmapData(symbol.getBounds(this).width,symbol.getBounds(this).height,true);
//The BitmapData Class contains pixels information for a bitmap.I created a bitmap data
//with width and height of the symbol. and set visiblity true.
var bitmap:Bitmap=new Bitmap(bitmapData);
//you know about this !
bitmapData.draw(symbol);
//The draw() method, does what you want.set pixels from a DisplayObject
//and use a matrix in parameters for the rotated,scaled,... form of the DisplayObject.
Now, The bitmap is ready.
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

Level of MovieClip

When creating some extended MovieClip to the stage using AS on Actions of some frame, on which layer this MovieClip is created? On layer where Actions were? If I wanna move it down, for example, can I simply move the Actions layer? And by the way, one more question, what if I would not create Actions layer and write all the code on some used one? :SS
whenever you add objects through ActionScript it will not consider your layers it will add on the top of the display list. and for traversing into movieclips u can use MoveiClip(root) or MovieClip(parent). check out addChildAt() and addChild() methods of ActionScript for more info.
Hope u mean nested MovieClips
http://www.quip.net/blog/2007/flash/trust-me-as3-its-a-movie-clip

How to copy Movieclip display properties as a vector into another movieclip

For my project I need to copy the graphics of a Movieclip with all of its children without copying anything else into a second Movieclip. I cannot use the Bitmap classes because I need the graphics to be displayed as a vector and I cannot use a method that simply copies the clip by calling the instructor ie:
var copy:MovieClip = clip.constructor
Is there any way to copy only the display portions of a clip into another Movieclip without turning it into a bitmap?
Thanks
Cloning display objects out of the box in as3 is no easy task. Here is a tutorial explaining the several approaches that have been attempted over the years and what works and what does not:
http://www.dannyburbol.com/2009/01/movieclip-clone-flash-as3/

Recreating stage layout in as3

I have 27 MovieClips in my Library which are all images. At the moment they are positioned on the stage as instances of their parent and then made to function in the first frame of my actions layer. I want to recreate this layout solely in code so there is nothing on the stage. How do I do this?
Thanks in advance.
Sam
Right click on a movieclip in the library, then go to Properties.
Tick "Export for ActionScript", then check the name where it says "Class". Hit OK.
Let's say this name was "Symbol1".
Then type this script:
var symbol1:MovieClip = new Symbol1();
addChild(symbol1);
var symbol1 means you created a variable, MovieClip is the type. This MovieClip variable is a "new " Symbol1 (this was the name in the library, Properties, Class.
Then add this to the stage:
addChild(symbol1)
If you want to position it on the stage, set the coordinates of the variable:
symbol1.x = 10;
symbol1.y = 10;
puts it to (10, 10).
Depending on how many objects you have you can type this code for each one of them (don't forget to Export them for actionscript in Library->Properties).
If you have tons of movieclips and you don't want to type evertyhing, but would rather write some dynamic code, give us a hint on your library structure and how you named your objects.
Hope this helps.