The Problems with Managing Depth in ActionScript



Riddle~

Is it important to keep track of depths in ActionScript for all of my display assets? It’s something extra to worry about, but there’s always the danger of killing something by overwriting the depth. It’s also important to have control on what graphics go in front of others. The “right way” is to have those depths explicitly defined along with each element so it’s clear who has what depth and it keeps the children from fighting. But then that doesn’t work for graphics placed on the stage at design time.

Answer:
Relational depth management.
This means, though it goes against all you believe, that you don’t track depth numbers. MovieClips, Buttons and TextFields are arranged around each other. This one goes in front of that one. That one goes behind these two.

This will sound flawed if you have been managing your own depths for a while. It feels like a bad practice. But when it comes down to it you have enough to keep track of, such as what your code is supposed to do (your model), to worry about managing one more interface detail. If you just try it out in a real-world setting it won’t be long before you’re hooked. If you haven’t been forced to manage depths a lot, then this will save you that hurtle so you can focus on learning more important “best practices”.

The XT DepthManager adds 5 public functions to the MovieClip, Button and TextField prototypes:

getNextDepth()
bringToFront()
bringForward([target])
sendBackward([target])
sendToBack()

Most of these are obvious from their names. They’re relational, so bringForward doesn’t move a MovieClip up one depth, it brings it in front of the graphic directly in front of it (even if both were placed on the stage during author time). The Flash IDE has a direct equivalent that most designers are familiar with, and it behaves identically. The one difference is the target that can be specified in the bringForward and sendBackward methods. This is where the real power of this solution lies.

target can be a visual element (MovieClip, Button or TextField), or a positive or negative number. bringForward will bring a MovieClip directly in front of the asset specified by target, while sendBackward will send it directly behind. A positive number in bringForward jumps n elements forward, and sendBackward jumps n elements backward. For example, my_mc.bringForward(3) moves my_mc in front of the 3 MovieClips directly above my_mc. my_mc.bringForward(1) is the same as my_mc.bringForward().

Still following? So last of all, a negative number passed to bringForward will actually start at the front of the stacking order and move back -n number of spaces. my_mc.bringForward(-1) will bring my_mc all the way to the front of the line minus one position, so ending up second in line. Consequently my_mc.bringForward(0) is the same as my_mc.bringToFront(). Using negative numbers in sendBackward will send the visual element to the back of the line minus n, or moving up. This concept can be difficult to understand at first. It is comparable to negative numbers used in String.substr(). Once these simple calls are understood, however, you have ultimate control in relational depth management.

get Flash to see this demo

The XT DepthManager is an efficient relational manager. It eliminates the depth complexities from your code and maintains good performance, adding < 1.5 KB to the total size of your SWF. For large numbers of assets in one parent, such as a listbox cell parent, it is still smart to set depths explicitly by number because it's faster and they correlate directly to an index, so you always know exactly where they're at. In these situations you don't care about stacking order anyway.

Download the XT DepthManager

The class available above is the basic relational depth manager which is compatible with Flash Player 6 and up. It will perform better in FP 7 as it takes advantage of MovieClip.getNextHighestDepth().
There is also a variant of the manager which additionally replaces the MovieClip and TextField creational methods (such as MovieClip.attachMovie() and MovieClip.createTextField()) so that depth management is abstracted (hidden) right from element creation. Never worry about depth issues again!



3 Responses to “The Problems with Managing Depth in ActionScript”

  1. Janos says:

    Hi, Does this work with negative timeline depths? Those in the -16,000 to -1 range? Also, could you post an FLA file with the zip showing how to import this ? I get the error : “‘com.codext.utils.Prototype’ could not be loaded.”

  2. xtyler says:

    Janos -

    hmm, somehow I had a mis-import in the DepthManager.as … all is fixed now. And yes, this class handles all elements that are placed on the stage in design time, which take up those negative depths. In fact it even (automatically) fixes the restriction of removing MovieClips when they’re in the negatives.

    good luck!

  3. Martin Scott says:

    Absolutely brilliant! It does exactly what it says on the tin!
    I have being using Flash with AS2 with versions 8 & 9 for a few years. I have always had problems with relative depths between text fields and movie clips. I hit a brick wall last week when trying to put a popup window containing text on top of other text fields and movie clips. I tried the recommended techniques, but got nowhere.
    I came across your XT DepthManager last night and it took me less than half a day to get the app working, including the popup window.
    I haven’t check AS3, so Adobe may have come up with its own solution, but for me bringToFront() and sendToBack() are ideal, and they’re all I need. I wish I’d found out about them sooner.
    Regards
    Martin Scott (martin@compan.net)

Leave a Reply

  • zoloft order
  • try viagra for free
  • buy medicine online
  • cholesterol care
  • alternative antibiotics
  • reducing cholesterol
  • bone loss treatment
  • cat products
  • reduce swelling methods
  • osteoporosis medication side effects
  • pets products
  • at home acne treatments
  • types of blood pressure medicine
  • cholesterol medication
  • vitamin nutrition supplement
  • buy birth control pills
  • order calcium carbonate
  • viagra and buy
  • reducing high blood pressure
  • order metformin online
  • buy prescription medication online
  • generic for viagra
  • accutane buy
  • skin solutions
  • cialis approval
  • treating chlamydia
  • fat loss tips
  • buy birth control pills
  • help for edema
  • cheap pain pills
  • healthy dog puppy food
  • new stop smoking medication
  • high cholesterol meds
  • healthy pet
  • vitamin metabolism
  • buy levitra onlines
  • lisinopril 5mg
  • new diabetes meds
  • arthritis in back
  • how to increase bust
  • drugstore online
  • weight loss help
  • cheap phentermine
  • canadian drug stores
  • cure for aids
  • zyban online from canada
  • cheap premarin
  • generic viagra lowest price
  • suffering produces endurance
  • longer lasting erection
  • viagra how works
  • skin cancer treatments
  • throat gonorrhea
  • information on congestive heart failure
  • new weight loss drugs
  • cialis blood pressure
  • preventing pregnancy after sex
  • stopping hair loss
  • online pharmacy in canada
  • price zyban
  • stop hair loss
  • quitting zyban
  • buy soma cheap online
  • list of prescription pain meds
  • treat high blood pressure
  • prescription med
  • drugs to help sleep
  • preventing pregnancy after sex
  • omega fish oil
  • senna constipation
  • order medication
  • how to relieve lower back pain
  • weight loss drinks
  • weight loss for women
  • safest weight loss pill
  • treatments for lung cancer
  • diabetes drug
  • treatments for high blood pressure
  • free weight loss program
  • nolvadex no prescription
  • anxiety insomnia
  • control premature ejaculation
  • causes erectile dysfunction
  • best hangover remedy
  • what is diazepam
  • body building product
  • clomid dosage
  • bipolar disorder
  • fast antibiotics cure chlamydia
  • buy singulair
  • aids for sleeping
  • hair loss cures
  • lisinopril 5mg
  • arthritis help
  • medication price
  • risperdal anxiety
  • blockers calcium channel
  • online soma
  • ambien 20mg
  • hair loss in children
  • best hair loss treatment
  • hand skin problems
  • good guide health womans
  • zyban pharmacy
  • buy singulair
  • what is generic viagra
  • chewable viagra
  • buy pain pills on line
  • new medicine for diabetes
  • osteoporosis therapy
  • strength training
  • tips to help loss weight
  • relieve pain in neck
  • increasing breast size naturally
  • new fda diet pill
  • vardenafil effectiveness
  • ambien 10
  • dog s health
  • severe leg muscle pain
  • dietary drug
  • alprazolam
  • allergies singulair
  • alternative therapy for rheumatoid arthritis
  • anxiety
  • new diet supplements
  • care dog skin
  • hair loss treatments for women
  • fluconazole interaction
  • carisoprodol price
  • online kamagra
  • infection lung
  • effective weight loss
  • lopressor drug
  • antibiotics gonorrhea
  • stress relievers
  • effexor dose
  • dog s health
  • generic online order viagra
  • buy generic cialis online
  • food allergies
  • scabies treatments
  • buy stopping diabetes
  • drugs for energy
  • repels insects
  • cialis on line
  • cheap baclofen
  • treatmemt of diabetes
  • treatment of obesity
  • where can i buy arthritis drugs
  • medical chlamydia
  • does levitra work
  • generic 5mg proscar cheap
  • cancer medications
  • prescription medication
  • abnormal blood clots
  • blood pressure treatment
  • cialis contraindications
  • online paxil
  • on line drug stores
  • control premature ejaculation
  • prozac depression
  • pain in chest
  • cheap canadian pharmacy
  • muscle and fitness
  • how to find medication prescriptions xanax
  • how do a stop smoking
  • simvastatin reaction
  • cheapest levitra
  • flu medicine
  • nausea cure
  • celebrex information
  • online pharmacy discount
  • online pharmacy discount drugs
  • over weight dog
  • diet and health products
  • lamisil generic
  • strength training
  • buy levitra online with fast delivery
  • weight loss drugs order
  • depression in men
  • dog health help
  • medication for depression
  • high blood calcium level
  • generic amoxicillin
  • cures for lung cancer
  • diet and health products
  • buy levaquin
  • drugs weight loss
  • prevention of hypertension
  • drugs for depression
  • sildenafil kamagra
  • cialis alcohol
  • singulair generic
  • pain management
  • wellbutrin cymbalta
  • buy zocor
  • cholesterol treatment
  • singulair generic
  • zoloft discount
  • european online pharmacy