A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: AS3 noob question

  1. #1
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967

    AS3 noob question

    Sorry to post this in the games forums, but you guys are speed/optimization oriented and I was looking for the right method to use in a game and not something that would just be done every 10 seconds or so (and the example I found seemed overly elaborate).

    I am new at AS3, so my obvious growing pains are the new event system and the new depth sorting. I am liking both so far over the inconsistency of AS2, but they are taking practice, and I am missing a lot of tricks. I know about making container sprites to hold basic layers (background, tiles, characters, interface), but sometimes you want to bring an object to the front of it's container.

    So, what is the 'best' way to move a movieclip to the top of the z index of the display list.

    1) The obvious question is, is there a swap method I have missed that will put it at the top? Or a function that will return the highest z for a given container?

    2) Is it reasonalbe to just remove the child and re-add it? This would put it at the top, and I don't think any associated properties would be lost since it would still be in memory after removeChild, right? Would this be slow if used a lot? Would it work? Would this cause it to flicker?

    3) On the web I found the following snippet. This seems like a time waster to me if you have a lot of objects like in an action game. But if option 2 is not feasible (or slow) it might be a solution.

    PHP Code:
    function docjslib_findHighestZ() {
         var 
    documentDivs = new Array();
         
    documentDivs document.all.tags("DIV");
         var 
    highestZ 0;
         for (var 
    0documentDivs.lengthi++) {
              var 
    Zindex documentDivs[i].style.zIndex;
              if (
    Zindex highestZ) {
                   
    highestZ Zindex;
              }
         }
         return 
    highestZ;

    What do you folks use?
    Last edited by Alluvian; 05-21-2009 at 10:15 AM.

  2. #2
    Developer dVyper's Avatar
    Join Date
    Oct 2008
    Location
    UK
    Posts
    168
    The easiest way as far as I know is your 1st point - to just remove it and then re-dd it to the container. You don't need to worry about speed, trust me.

  3. #3
    Pencil Farmer cadin's Avatar
    Join Date
    Jul 2006
    Location
    Vancouver BC
    Posts
    323
    1. Yes and yes.
    You can get the highest Z within a container with numChildren.
    You can set a display object to any depth by using setChildIndex.

    So something like this should pop it to the top of the display list.
    PHP Code:
    container.setChildIndex(myMCcontainer.numChildren-1); 
    AS3 handles all the depth sorting for all the other children which is really nice.


    2. I don't know if there's a speed advantage to either.

    3. That looks like javascript.

  4. #4
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    Awesome, can't believe I missed .numChildren
    Just not thinking in AS3 yet I guess. Definately looking forward to AS3, even ignoring the speed boosts, the language is structured so much better if I can just get over the learning curve. No formal programming training means I have a lot of odd gaps in my knowledge. Never working on one language for more than a few months does not help.

    So, the following should also work to move 'myMC' to the top of it's parent?

    myMC.parent.setChildIndex(myMC, myMC.parent.numChildren-1);

    In my current game my containers are pretty straightforward, but may not be for other games like something with paralax scrolling.

  5. #5
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    You just add the movie clip, no need to set any index and no need to remove it first. It is always placed on top of other children and if it was already children previously, its depth is just changed to top. So its simply

    container.addChild(myMC);

  6. #6
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    Nice!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center