getting movieclip on top of everything else - actionscript-3

i have a bunch of movieclips on the stage and some are nested into others, i would like to have 1 of them to be put on top of everything else on the stage when i start dragging it. Anyone know how to get a nested movieclip on top of parents etc?

You could have a top layer & a bottom layer , whenever you want to add a child on top of everything else, simply add the child to the top layer. This way you don't need to figure what the parent position is , just keep this layer on top for that specific purpose.
var topLayer:Sprite = new Sprite();
var bottomLayer:Sprite = new Sprite();
var childMc:MovieClip; // the mc you want to have on top
//somewhere in your code...
childMc.addEventListener(MouseEvent.MOUSE_DOWN , childMcMouseDownListener );
addChild( bottomLayer );
addChild( topLayer );
//add everything to the bottom layer
//leave the topLayer empty until you add the child mc
//add the following code in your MouseDown Listener
function childMcMouseDownListener(event:MouseEvent ):void
{
topLayer.addChild( event.currentTarget );
}

top of everything else on stage:
stage.addChild(target);
warning from AS3 doc :
The command stage.addChild() can cause problems with a published SWF
file, including security problems and conflicts with other loaded SWF
files. There is only one Stage within a Flash runtime instance, no
matter how many SWF files you load 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. Create a
DisplayObjectContainer to contain all of the items on the display
list. Then, if necessary, add that DisplayObjectContainer instance to
the Stage.

Suppose, Container is your parent movieClip and it has many children then Now your target and currentTarget property depends on which object the listener is attached and how much you have nested your movieclips. in my case the Container is your parent movieClip having two or more children.
Container.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
public function pickUp(param1:MouseEvent) : void
{
var objectToDrag = param1.target;
Container.setChildIndex(param1.target,Container.numChildren-1);
objectToDrag.startDrag(true);
}

Related

Add Child Behind existing object AS3

I have made a game using Flash CS5 (as3) and I am trying to add a child to the stage behind objects that are already there. For example, I have a bar at the bottom of the screen that is there from the time you start the game and I have falling objects that I want to fall behind it, but instead they fall in front of it because I added them to the stage after. Is there a way of adding the falling objects behind it without having to keep re-adding the bar to the stage? Thanks in advance.
Rather than layering, I'd use adjust the index of each object using addChildAt and setChildIndex.
The following line adds your falling object behind every other DisplayObject on the stage (in this case, you should probably add your bar to the stage first)
stage.addChildAt(fallingObject, 0);
You can create Sprites to act as layers, and add the different objects to them. Here is an example that adds a layer for adding whatever you want behind the bar layer, and THEN adds the bar layer, so it will be on top. It's super rough since you don't have any code to reference:
package {
import flash.display.*;
public class Test extends MovieClip {
var barLayer:Sprite;
var objectLayer:Sprite;
public function Test() {
var objectLayer = new Sprite();
//add the object layer to your main Class
this.addChild(objectLayer);
//now you can add movie clips or sprites to objectLayer whenever you like
//then create the bar layer
var barLayer = new Sprite();
//add your bar Display Object here
var bar = new MovieClip();//draw the bar or reference the library object you've created
barLayer.addChild(bar);
//now add the barLayer
this.addChild(barLayer);
}
}
}

AS3 setChildIndex to front

Is there a way to send a specific movieClip to the front of all other movieClips on stage?
I know about setChildIndex, but I can't figure out a way to to calculate the top position dynamically.
You can use setChildIndex() with numChildren.
setChildIndex(childClip, numChildren - 1);
You don't need to use setChildIndex.
addChild is sending child on the top of all clips.
You may be thinking that addChild will double add your MC, but it will not, the second time it will just update it's child index.
By default, flash adds the movieclip to the top of the display list, so you can just repeat adding of the child to the container.
stage.addChild(bottom);
stage.addChild(top);
// top is now on bottom.
stage.addChild(bottom);
// bottom is now on top.
The movieclip(mc) is added on the canvas,
canvas.setChildIndex(mc, 0);
The canvas is added on the stage,
stage.setChildIndex(canvas, 0);
/* Bring Any Clicked Object to the Front
Clicking on any symbol on the Stage moves it in front of all other instances.
*/
// This code makes all symbol instances on stage clickable by making them listen for the CLICK event.
for (var fl_ChildIndex:int = 0;
fl_ChildIndex < this.numChildren;
fl_ChildIndex++)
{
this.getChildAt(fl_ChildIndex).addEventListener(MouseEvent.CLICK, fl_ClickToBringToFront);
}
// This is the function that moves the clicked object to the front of the display list
function fl_ClickToBringToFront(event:MouseEvent):void
{
this.addChild(event.currentTarget as DisplayObject);
}
From Adobe Flash Professional Code Snippets.

How can I define what a layer a child is created onto when using Flash CS5 AS3?

I need to display a child on layer 2. How would I, using AS3, dynamically create a child on frame 2?
"Layer 2" isn't specific enough. What layer of what display object container?
Here is a good article on display list programming that should prove enlightening.
To answer your question, you would identify the parent displayObject and call its addChild method, with your targeted child displayobject as the parameter. If your parent displayObject is the containing class (a class that extends DisplayObject, a Sprite, for example), you can just call addChild() or this.addChild(). To add the child at a layer other than the topmost, you can use addChildAt().
var someclip:Sprite = new Sprite();
var someOtherClip:Sprite = new Sprite();
var yetAnotherClip:Sprite = new Sprite();
var someLibrayClip:LibraryClip = new LibraryClip();
this.addChild(someClip);
this.addChildAt(someOtherClip,0);
someOtherClip.addChild(yetAnotherClip);
someOtherClip.addChildAt(someLibrayClip,0);
etc...
Note that the display list is a stack like an array, and in its case, may not contain empty indexes. If you want something in layer 2, there must also be items in 0 and 1.
Hope that helps -
Layers only exist in the Flash IDE. They are not part of Flash Player's display list system. So you can't specify what layer a child goes into. Use addChild() or addChildAt() to add children to containers.
Create a symbol in the library with nothing in it. I normally call these empty.
Place an instance of this symbol on stage where you need it. Give the instance a name. For sake of this answer, it is empty_mc.
Then in AS do
var whatever : Whatever = new Whatever();
empty_mc.addChild(whatever);
at the appropriate place.

Movieclip stacking in Actionscript

I'm building a game of which the interface is one of the first items to load on screen. Sound button, pause and all the bits -
During the game - all manor of things are dynamically added and removed to the stage. Therefore my interface goes behind my game scene.
How do I ensure the movieclip always stays on top?
Can I override the addChild of my Document Class and every time a new child is added, I restack the interface to the top?
You can use setChildIndex to rearrange objects that are already contained within a MovieClip or Sprite. For example, if you have an object called container, which contains a redBall, blueBall and many other ball objects, you can use the following to make sure redBall is behind everything else:
container.setChildIndex(redBall, 0);
Equally you can make sure that blueBall will be displayed infront of everything else using:
container.setChildIndex(blueBall, container.numChildren-1);
addChildAt will sort you out for adding children straight into their correct position, and setChildIndex will allow you to re-position objects already placed. Hope that helps.
debu
Look into addChildAt, it allows you to specify the index that the new object should be added too.
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/DisplayObjectContainer.html#addChildAt%28%29
A good strategy is to keep all interface elements in one parent Sprite/MovieClip, and all game assets in another. (With the interface one on top)
With your guys suggestion I made this, apparently, if you addChild an object already on the screen, it's simply reindex'd to the top. Does this look ok?
private var topLevelChildrenArray:Array = [];
public function addTopLevelChild(child:DisplayObject):DisplayObject
{
topLevelChildrenArray.push(child)
return this.addChildAt( child, this.numChildren - 1 );
}
override public function addChild(child:DisplayObject):DisplayObject
{
this.addChildAt(child, this.numChildren);
for each(var topChild:DisplayObject in topLevelChildrenArray)
{
super.addChild(topChild);
}
return child
}

Deleting a shape via code

Pretty basic question here, but its still got me a little confused..
I have an object(navigation menu bar) that I want to change the colors on with code, so in an updateColor function, I get the bounds of the object (which is a drawing shape contained in a movieclip) and redraw a new shape on top of it with the new color, but I've noticed that the last shape still exists behind this redraw.
I tried using obj.graphics.clear(); before the redraw but that didn't get rid of the original shape. Is there another command that I'm overlooking?
Unless you drew the object you wish to remove within the same graphics object, clearing won't work. You need to remove the DisplayObject.
Depending on the number of children you can do:
obj.removeChildAt(0);
This also removes movieclips / buttons you placed on the stage manually.
If you have a reference to the DisplayObject you wish to remove you can simply do
obj.removeChild(backgroundClip);
Note that you can also change the color of a DisplayObject directly:
import flash.geom.ColorTransform;
...
public var test:MovieClip; //instance on stage
...
var cf:ColorTransform = test.transform.colorTransform;
cf.color = 0xff0000;
test.transform.colorTransform = cf;
while(this.numChildren)
{
this.removeChildAt(0);
}
Will clear child object on this MovieClip,
if it's clearing too much, then put the shape drawing in a subclip, and clear the subclip.