Display Flash frames in random order

January 15, 2010

Actionscript 3.0 Tutorial

Display frames in random order.

Download source project  here (3 kb)

  1. Launch Flash Designer and choose Blank
  2. Set movie dimensions 230 x 100 (or any desired) with Frame > Frame Size
  3. Change frame delay to “0.05″.
  4. Choose “Frame” > “ActionScript” and paste the code:
    // generate random number (f) but different than the last one (l)
    // the number will be between 1 and 6
    
    while(f==l) { f = Math.Floor(Math.Random()*6)+1; }
    
    // remember the last random number
    l = f;
    
    // jump to a frame
    if(f==1) gotoAndPlay("Frame 2");
    if(f==2) gotoAndPlay("Frame 3");
    if(f==3) gotoAndPlay("Frame 4");
    if(f==4) gotoAndPlay("Frame 5");
    if(f==5) gotoAndPlay("Frame 6");
    if(f==6) gotoAndPlay("Frame 7");
    
  5. Choose “Frame” > “Insert”, put Qty: 6 and delay 2 seconds
  6. Put graphics on each frame (from Frame 2 to Frame 7) for example numbers
  7. Hit F9 for preview.
, , ,

Subscribe

Subscribe to our e-mail newsletter to receive updates.

2 Responses to “Display Flash frames in random order”

  1. laura Says:

    This example has been the best match for what I am trying to accomplish in a banner ad I am working on! I ran into one problem with my test file:

    - I am using Flash CS5 with AS2.
    - everything in my test file went smoothly except the random order did not work (my frame sequence ran 1,2,3,4,5,6,7, repeating)
    - the AS compiler error I received was:
    Scene=Scene 1, layer=actions, frame=1, Line 4 There is no method with the name ‘Floor’.
    - I also tried recreating the test file using AS3, but encountered more compiler errors =(
    - here is the (same as above) code I copied & pasted:

    // generate random number (f) but different than the last one (l)
    // the number will be between 1 and 6

    while(f==l) { f = Math.Floor(Math.Random()*6)+1; }

    // remember the last random number
    l = f;

    // jump to a frame
    if(f==1) gotoAndPlay(“Frame 2″);
    if(f==2) gotoAndPlay(“Frame 3″);
    if(f==3) gotoAndPlay(“Frame 4″);
    if(f==4) gotoAndPlay(“Frame 5″);
    if(f==5) gotoAndPlay(“Frame 6″);
    if(f==6) gotoAndPlay(“Frame 7″);

    If there is anything I did wrong or any changes I need to make, please advise. Thanks!!!

  2. laura Says:

    P.S.
    The compiler error went away after changing 2 upper case letters (Math.Floor and Math.Random) to lower case Math.floor and Math.random.

    Unfortunately my frame sequence is still the same (rather than random).

Leave a Reply