A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Removing and adding MovieClips to/from arrays based on a variable?

  1. #1
    Junior Member
    Join Date
    Oct 2008
    Posts
    21

    Removing and adding MovieClips to/from arrays based on a variable?

    So a small example of my situation...On my stage I have 5 items labeled item1_mc, item2_mc, item3_mc and so forth...they all have two keyframes on their respective timelines, one labeled "active" and the other "inactive". My code contains 2 arrays, inactiveArray and activeArray and a variable known as energy that regularly goes to and from 0 - 100.

    What I am looking to achieve is basically the function of...

    if energy >= "insert given items 'active threshold' 20, 40, 60, etc..." then add it to the activeArray, if it is not, remove it and add it to the inactive array.

    all objects in activeArray gotoAndStop("active");

    all objects in inactiveArray gotoAndStop("inactive");

    I have tried many different ways of achieving this effect but I always end up with duplicates and extras or something doesn't move when it's supposed to, where it's supposed to, it just ends up into a giant cluttered mess and I start from scratch.

    I really dislike "giving up" but I am completely stumped and really need help with this one...any and all assistance will be very very VERY appreciated!

  2. #2
    Senior Member
    Join Date
    Jan 2008
    Location
    UK
    Posts
    269
    Do you really need to maintain two arrays for storing the acive/inactive status? I would simplify things a little by having just one array which contains a list of your movieclips, and then store the active/inactive status as a property of the movieclip.

    Code:
    //start by putting each mc into array
    var mcArray:Array = [item1_mc, item2_mc, etc.....];
    
    //set active/inactive according to your threshold conditions
    for(var i:uint = 0; i < mcArray.length; i ++){
        if(your condition goes here == true){
            mcArray[i].active = true;
        }else{
            mcArray[i].active = false;
        }
    }
    
    //code for setting correct frame of each movieclip
    for(var n:uint = 0; n < mcArray.length; n ++){
       if(mcArray[n].active == true){
            mcArray[n].gotoAndStop("active");
        }else{
            mcArray[n].gotoAndStop("inactive");
        }
    }

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You can even combine those into a single pass. And if the clips don't need to do anything else with the active state, you don't need to explicitly store it.

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