A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Random Frame Array

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    19

    Random Frame Array

    I have a Jukebox that randomly plays songs based on frame labels. What I have right now is:
    Frame 1 has;
    var aLabels = new Array("L1","L2","L3","L4","L5","L6","L7");

    I have a button on each frame that has;
    on (release) {
    var nRandom = Math.floor(Math.random() * aLabels.length);
    gotoAndStop(aLabels[nRandom]);
    }

    What I need is to remove each label from the array when you click the button to randomly generate the next song (label). So when you land on "L2" and click "Next Song" it will remove "L2" from the array so it doesn't play again. When all the labels are removed from the array, it jumps to frame 15 and stop.

    Can anyone offer some help?

  2. #2
    Senior Member Robb@exo's Avatar
    Join Date
    Jun 2003
    Location
    Canada
    Posts
    1,540
    Try looking into using the "splice" property of the Array class. It removes a defined amount of indicies from the array.
    Actionscript Code:
    var aLabels = new Array("L1","L2","L3","L4","L5","L6","L7");
    on (release) {
        var nRandom = Math.floor(Math.random() * aLabels.length);
        aLabels.splice(2, 1); // the first digit "2", refers to the related index that is being identified, in this case "L3"
                                    // the second digit "1", refers to the length to be removed, in this case only "L3" would be removed.
        trace(aLabels);
    }

    HTH.
    Wile E. Coyote - "Clear as mud?"

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    19
    The array I have works just fine. My request is to have it jump to a specific frame or label after the array is empty. Since this post though, I have inserted the "splice" code and that also functions fine. However, when the array is empty it simply "stops" rather than indicating (by going to a specified frame/label) the array is empty. Instead, you assume it "froze" or just stopped working.

  4. #4
    Senior Member Robb@exo's Avatar
    Join Date
    Jun 2003
    Location
    Canada
    Posts
    1,540
    Once the array is empty, repopulate it with the original definition. ie,
    Actionscript Code:
    aLabels = ["L1","L2","L3","L4","L5","L6","L7"];

    Does this help?
    Wile E. Coyote - "Clear as mud?"

  5. #5
    Junior Member
    Join Date
    Feb 2010
    Posts
    19
    That's good but not what I want. I don't want it to keep looping. I want it to go to a specific frame where I would have something on the stage stating to the array is empty. Basically, the program/game/movie/etc is over. To close and reload to play again. Something similar to that.

  6. #6
    Senior Member Robb@exo's Avatar
    Join Date
    Jun 2003
    Location
    Canada
    Posts
    1,540
    Then use a conditional statement,
    Actionscript Code:
    if(aLabels.length == 0) {
        gotoAndPlay("FrameLabel");
    }
    Wile E. Coyote - "Clear as mud?"

  7. #7
    Junior Member
    Join Date
    Feb 2010
    Posts
    19
    I'll give that a try. Thank you.

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