Bring the object to the front, in Flash AS3

I have the code below which basically animates the object across the screen, when the switch happens, it pauses the animation and displays some information. Everything works fine, but when it paused, I hugged how this current object is to be "on top" so that other objects lag behind.

I looked at setChildIndex but haven't had much luck.

package {

import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.events.KeyboardEvent;
import flash.events.*;
import caurina.transitions.Tweener;
import fl.motion.Color;

public class carpurchase extends Sprite {

    public function carpurchase() {

        var carX = 570;


        //Set cars
        var car1:fullCar = new fullCar();
        car1.info.alpha = 0;
        //var c:Color = new Color();
        //c.setTint(0xff0000, 0.8);
        //car2.car.transform.colorTransform=c;
        car1.x = carX;
        car1.y = 280;
        car1.info.title.text = "test";
        car1.info.desc.text = "test";
        addChild(car1);
        car1.addEventListener(MouseEvent.ROLL_OVER, carPause);
        car1.addEventListener(MouseEvent.ROLL_OUT, carContinue);
        function car1Reset():void {
            Tweener.addTween(car1, {x:carX, time:0, onComplete:car1Tween});
        }
        function car1Tween():void {
            Tweener.addTween(car1, {x:-120, time:2, delay:3, transition:"linear", onComplete:car1Reset});
        }
        car1Tween();


        var car2:fullCar = new fullCar();
        car2.info.alpha = 0;
        var c:Color = new Color();
        c.setTint(0xff0000, 0.8);
        car2.car.transform.colorTransform=c;
        car1.x = carX;
        car2.y = 175;
        car2.info.title.text = "test";
        car2.info.desc.text = "test";
        addChild(car2);
        car2.addEventListener(MouseEvent.ROLL_OVER, carPause);
        car2.addEventListener(MouseEvent.ROLL_OUT, carContinue);
        function car2Reset():void {
            Tweener.addTween(car2, {x:carX, time:0, onComplete:car2Tween});
        }
        function car2Tween():void {
            Tweener.addTween(car2, {x:-120, time:3, delay:0, transition:"linear", onComplete:car2Reset});
        }
        car2Tween();



        function carPause(e:MouseEvent):void {
            Tweener.pauseTweens(e.target);
            Tweener.addTween(e.target.info, {y:-150, alpha:1, time:.5, transition:"easeout"});
        }   
        function carContinue(e:MouseEvent):void {
            Tweener.addTween(e.target.info, {y:10, alpha:0, time:.5, transition:"easeout"});
            Tweener.resumeTweens(e.target); 
        }
    }
}

      

Any help is appreciated

+2


a source to share


5 answers


Just use addChild () for that specific instance. It will be placed at the top

From the help files:



Adds a child DisplayObject instance to this DisplayObjectContainer instance. In this DisplayObjectContainer instance, the child is added to the front (top) of all other children .

And yes, it works even if the object is already in the display list.

+8


a source


Have you tried setChildIndex ?



+4


a source


function carPause(e:MouseEvent):void {
    setChildIndex(e.target, numChildren-1);
    Tweener.pauseTweens(e.target);
    Tweener.addTween(e.target.info, {y:-150, alpha:1, time:.5, transition:"easeout"});
} 

      

+3


a source


I was looking for the same problem and found this old post. I solved this problem in a nice way and just want to share it for people to find this post.

The problem was that you are trying to install childindex from the loader object itself, but that is worng location to install it. You need a parent path to access all your cars in this case.

Well, long story: Try these changes: Create a new Sprite in the attribute feed (after the class name)

// [...]
public class carpurchase extends Sprite {
   private var _sprite:Sprite = new Sprite();
}
// [...]
// Add the cars to the sprite
  _sprite.addChild(car1); // replace this lines
//[...]
  _sprite.addChild(car2); // replace this lines
// Of course adding the sprite to your stage
  addChild(_sprite);

      

After that, you will access the current target by selecting it by name and set it on top

_mc.setChildIndex(_mc.getChildByName(e.target.name), _mc.numChildren-1); // Set on top

      

Maybe it helps someone someday

+2


a source


Have you tried addChildAt

?

Adobe Live Docs - addChildAt ()

+1


a source







All Articles