Quick and Easy Depth Tricks

Tweet

I was rooting around in the Flash MX 2004′ sock drawer and stumbled across a rather nifty AS file (DepthControl.as) buried in the installation directory; a little further exploratory work and i was pleasantly surprised to find some very simple methods for depth shuffling (kinda cool if you’re into game or windowed application development).

Here’s the list of publically available functions for the class:


bringForward(target:MovieClip)
sendBackward(target:MovieClip)
bringToFront(target:MovieClip)
sendToBack(target:MovieClip):Void

And having created a simple movie clip with a linkage identifier of depthTest, I added the following code to the first frame of the timeline allowing simple press-release functionality to the duplicated MCs on the stage


for (i=0; i<15; i++) {
duplicateMovieClip("depthTest", ["depthTest"+i], i+5);
_root["depthTest"+i]._x = 50+(i*10);
_root["depthTest"+i]._y = 50+(i*10);
_root["depthTest"+i].onPress = function() {
mx.behaviors.DepthControl.bringForward(this);
};
_root["depthTest"+i].onRelease = function() {
mx.behaviors.DepthControl.sendBackward(this);
};
}

Now when I need to include any depth manipulation in my applications, I can simply include the class and call the functions through the prototype chain!

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • A. White

    Is there any place that documents all of the mx.behaviors in detail?

  • dawgbone

    A. White… it’s called the internet.

    All joking aside, a detaile breakdown of the behaviours would be insanely sweet.

  • http://www.simonjobling.com Si

    Have you tried any of the Friends of Ed references? I’ve got the Flash MX Actionscript Reference by them guys and it’s a great resource. I’m sure the MX2004 edition is even better! (BTW, I’m not connected to FoE – just really impressed with their stuff.)

  • _lennel_

    the bring to front method is extremely slow.

  • sgrosvenor

    Anyone else having speed issues?, as i’m unable to reproduce the speed issue here!

  • zakcoop

    I haven’t noticed speed issues except … I have noticed that when other press/release actions are assigned to the same button things can pause for a moment before executing.

    I’m working on a new web site and have found these functions to be invaluable. Though, I sure would love a way to cross-fade the depth change of layers. I’ve simply worked around this using an equally useful (perhaps more) tween class that provides great functions as well.

    http://www.inlandoffice.com/prototype

    The tween class can be found at:

    http://laco.wz.cz/tween

    Best,

    Aaron

  • ktec

    Nice find, thanks

  • mediamogul

    THANK YOU!!!!!
    I’ve been struggling with bringing movieclips to the front for days now… :P
    I had it working fine in AS 1.0, player #6, but in AS 2.0 it suddenly seemed impossible. Until now. :D
    once again, cheers, you made my day!
    regards,
    mediamogul

  • mjchael

    that the class have to be import should be mentioned
    mx.behaviors.DepthControl;
    mx.behaviors.DepthControl.sendToBack(myMC);
    //…bringToFront(myMC);
    // ….sendBackward(myMC);
    // ….bringForward(myMC);

  • mjchael

    sorry,
    import mx.behaviors.DepthControl;