A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: random frames

  1. #1
    Junior Member
    Join Date
    Jun 2000
    Posts
    11
    I have a 30 frame movie and i want a button in each frame that makes the movie jump to another random frame. The trouble is I don't want any of the frames repeated.

    Any ideas greatly appreciated

  2. #2
    Member
    Join Date
    Aug 2001
    Posts
    96
    See whether this can help you...

    1. Assign an array of 30.

    2. Randomly generate a number between 0-29 (or 1-30 (just add one)) and assign this to a vaiable "frameno".

    3. Put the value of "frameno" into the first array elment.

    4. Goto that frame.

    5. Randomly generate another number.

    6. Check whether the number is the same as the element in the array.

    7. Goto that frame.

    8. Repeat steps 5-7.

    Hope it helps...

  3. #3
    Junior Member
    Join Date
    Jun 2000
    Posts
    11
    I don't really know about using arrays so i'll have to learn.

    Thanks for the help!

  4. #4
    Senior Member
    Join Date
    Jan 2001
    Posts
    472
    You need an array to record which frames have been visited. First frame of your movie:

    totalFrames = 30;
    beenThere = new Array(totalFrames+1); //array will hold values for frames 1 through totalFrames
    beenThere[1] = true; //records visit to first frame

    // function to run through array to make sure there are frames left to visit
    function CheckArray() {
    for (var j = 1; j < beenThere.length; j++) {
    if (!beenThere[j]) { // if not visited
    return true
    }
    }
    return false; // if no frames left to visit
    }

    stop();


    Now on your buttons:

    on (release) {
    if (CheckArray()) { // if there are frames left
    do {
    frameTest = Math.ceil(Math.random()*totalFrames); // find a random frame number
    } while (beenThere[frameTest]); // check the corresponding array index to see if it's been visited
    beenThere[frameTest] = true; // place a true value in array to note that we're going there
    gotoAndStop (frameTest); // go there
    } else {
    gotoAndStop("end") // go to end label if all frames have been visited
    }
    }


    Look it over and let me know if you've any questions on that.


    --tyard
    http://www.27Bobs.com

  5. #5
    Junior Member
    Join Date
    Jun 2000
    Posts
    11
    fantastic,

    thanks guys

  6. #6
    Senior Member
    Join Date
    Apr 2001
    Posts
    528

    What About

    Tyard,

    I am building an on-line exam that will randomize questions. Your code for goangus was great. My users want to be able to go back through their answer when finished. So my question is...Since the Array holds the frame's it has been to, would I use a button action that is beenthere [frametest -1]?

    Any direction you could point me would be helpful.

    Thanks.

  7. #7
    Senior Member
    Join Date
    Jan 2001
    Posts
    472
    well, since frameTest holds all the frames in order of visitation, go to the beginning of the array. First, though, you have to remove the empty element at index 0 (since we didn't store anything there):

    frameTest.shift();

    Now we can step through the array. The easiest way to do it is to use the same shift() method:

    gotoAndStop(frameTest.shift());

    shift() will not only return the first element in the array, it will also remove that element. So if our array is:

    frameTest = [5, 8, 2, 6, 1];

    gotoAndStop(frameTest.shift());

    will send the playhead to frame 5 and remove 5 as the first element. The next time you acces frameTest, it will equal:

    [8, 2, 6, 1]

    So just keep stepping through until the array is empty. If you want to retain the values in frameTest, just do the revisitation with a duplicate:

    review = frameTest.slice();

    How's that?


    --tyard
    hhtp://www.27Bobs.com

  8. #8
    Senior Member
    Join Date
    Apr 2001
    Posts
    528
    Thanks for the prompt response. I will give it a shot.

  9. #9
    Senior Member
    Join Date
    Apr 2001
    Posts
    528
    Well, this is what I have:

    stop ();
    totalFrames = 18;
    frameTest.shift();
    beenThere = new Array(totalFrames+1);
    // array will hold values for frames 1 through totalFrames
    beenThere[1] = true;
    // records visit to first frame
    // function to run through array to make sure there are frames left to visit
    function CheckArray () {
    for (var j = 1; j<beenThere.length; j++) {
    if (!beenThere[j]) {
    // if not visited
    return true;
    }
    }
    return false;
    // if no frames left to visit
    }
    gotoAndStop (frameTest.shift());
    stop ();


    Is it possible I put the frameTest.shift() in the wrong area?

    On my button I have

    on (release) {
    gotoAndStop (frameTest.shift());
    }

  10. #10
    Senior Member
    Join Date
    Jan 2001
    Posts
    472
    Well, I/we got confusedd between the two different above explanations-- in the first, frameTest is a number variable, not an array. beenThere is the only array.

    Basically, you create an array called beenThere to hold your frame values. Don't use shift() yet-- you only do that at the end when you want to go back through the array. So pretty much go through my first explanantion to visit all of your frames once and place their values in the beenThere array.

    Okay, so now at the end of your test (remember, we've yet to do any of our shift() methods on our array, and we've only visited each frame once) we should be on our end frame, and tracing beenThere should reveal something like:

    ,3,5,1,7,8,4,9,2,6 // if totalFrames = 10

    First index is blank, right (since we never visit frame 0)? So NOW let's get rid of that first index:

    beenThere.shift();

    Now our beenThere array will trace as:

    3,5,1,7,8,4,9,2,6

    and we can step through it one index at a time:

    gotoAndStop(beenThere.shift());

    Am I making better sense now?


    --tyard
    http://www.27Bobs.com

  11. #11
    Senior Member
    Join Date
    Apr 2001
    Posts
    528
    excellent. It works!

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