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

  • drugs hair loss
  • antibiotic
  • side effects of antibiotics
  • nexium health
  • new medication for cancer treatment
  • congestive heart failure overview
  • canada cialis generic
  • appetite suppressant
  • pain med without prescription
  • buspar online
  • medication pediatric insomnia
  • very cheap viagra
  • increase womens sex drive
  • lowest price generic viagra
  • buying online viagra
  • cheap alcoholism treatment
  • information on ambien
  • celexa and anxiety
  • lamictal withdrawal
  • celebrex capsules
  • diabetes treatment
  • pain meds no prescription
  • weight loss food
  • body building diets
  • drugs fda
  • anti-fungal
  • pharmacy allegra
  • parkinsons disease medication
  • reducing high blood pressure
  • how does viagra work?
  • medications for depression
  • clomid cycles
  • best male enhancement drugs
  • bodybuilding nutrition buy supplements
  • natural dog health
  • next day medication
  • herbal weight loss products
  • weight loss nutrition
  • famvir dose
  • cheap antibiotics
  • order zoloft
  • zyrtec medication
  • acne skin treatment
  • prescription pain medicines
  • wal mart pharmacy
  • cheap nolvadex
  • online phentermine online prescription
  • tips for insomnia
  • stress medicine for cats
  • canada online drug stores
  • osteoporosis evista
  • viagra order online
  • drugs online no prescription
  • cheap birth control online
  • newest bodybuilding products
  • treating diabetes
  • deep sleep disorder
  • celexa buy
  • how to lose asthma
  • rheumatoid arthritis medications
  • xanax sales
  • high cholesterol diet
  • anxiety order
  • canada pharmacy
  • vitamine guide for dogs
  • different treatments of alcoholism
  • online apotheke
  • hypertension and low heart rate medications
  • treatment for hepatitis c
  • list of blood pressure medicines
  • information on sleeping aids
  • cheap antibiotics
  • bust enlarge
  • how to get viagra prescription
  • muscle nerve pain
  • mens health 40
  • hair re-growth
  • acai berry products
  • tips for gaining muscle mass
  • where to order soma
  • baby acne
  • cialis 10
  • viagra discount online
  • sleeping problems uk
  • buy cialis without a prescription
  • free stop smoking ways
  • womens health menopause
  • how to relieve ear pain
  • hoodia fast
  • aldactone medication
  • uric acid remedies
  • generic zyrtec
  • hangover help
  • buy ambien online cheap
  • lexapro paxil
  • sinus infection med
  • dog lose weight
  • order birth control
  • anti-fungal
  • neck pain
  • psoriasis treatments
  • cialis on line
  • pravastatin
  • new heart attack drugs
  • how to cure diarrhea
  • online pharmacy ratings
  • generic allegra
  • ativan prescription
  • prescription for severe stomach pain
  • medical irritable bowel
  • teeth whitening buy
  • women's hair loss remedie
  • why most people benefit from antidepressants
  • pain cure
  • klonopin anxiety
  • hiv medications
  • prevent itching
  • xanax buy on line
  • nolvadex no prescription
  • xanax perscription
  • anxiety disorder
  • canadian arthritis medication
  • menstrual cramp pain
  • stomach ulcer pain
  • dietary supplement
  • acai berry pills
  • viagra coverage california
  • cures for depression
  • lower your blood pressure
  • treatment of psoriasis
  • cold sores help
  • buy Lozol
  • high blood pressure and treatment
  • pet health care
  • diet supplements distributors
  • cyclophosphamide
  • get klonopin
  • anti-fungal
  • shoulder arthritis
  • cheap caffeine
  • acne tips
  • free prescription medication
  • cat anxiety medicine
  • hepatitis c treatments
  • medication for severe chronic pain
  • order prozac
  • blue pills
  • parkinson's meds
  • anti malaria drugs
  • diet pills online
  • online drug store
  • vascular edema
  • online valium no prescription
  • help stop smoking
  • hydrochlorothiazide generic
  • medication prices
  • cold v flu
  • congestive heart failure med
  • blue pills
  • kidney cancer drugs
  • chlamydia cures
  • vitamin c for dogs
  • oral diabetes medication
  • us drugstore
  • ultimate weight loss
  • list pain meds
  • cialis best on-line drugstore
  • ambien sample
  • new treatment for heart failure
  • pet meds on line
  • buy clomid
  • cheap cialis site
  • bodybuilding nutrition
  • small penis
  • ambien dosages
  • treatments and cures for epilepsy
  • natural weight loss supplements
  • dry skin treatment
  • online order ambien
  • symptoms of stop smoking
  • blood pressure treatment drugs
  • diclofenac dosage
  • helping high blood pressure
  • bronchitis medications
  • antibiotics safe for dogs
  • immune system strengthen
  • best weight loss pills
  • usa pharmacy
  • allied health
  • colon cancer treatment
  • drug treatment for alcoholism
  • tramadol very cheap
  • early prostate cancer treatment
  • side effects claritin
  • retin a cheap
  • online pharmacies in mexico
  • new treatment for allergies
  • prescription drug prices
  • generic plavix
  • buying viagra prescription