Website Designers and Webmasters

Dedicated to all the tasks Webmasters, Website Developers and Website Designers find themselves facing.

Flash Random Scene ActionScript

I recently created a Flash Animation that would randomly play one of three different Flash scenes. Then after each scene, it would go back to the beginning and randomly choose another scene to play. This was all programmed in ActionScript in Flash.

I have a feeling that even though this code is very simple, it will save a little bit of time if I have it stored here for the future.

// First Choose a random number
//from 0 to the number of scenes
num = random(3);

//Depending on which random number
//was chosen, go to a flash scene
switch (num)
{
case 0:
gotoAndPlay(“scenename”, 1);
break;
case 1:
gotoAndPlay(“scenename1″, 1);
break;
case 2:
gotoAndPlay(“scenename2″, 1);
break;
default:
trace ( ” no case tested true ” )
}

That’s it!
-Ashton Sanders

(P.S. If this was useful for you, please leave a comment! ;))

Tags: , , , ,

8 Comments »

  1. hi ashton, looks good i was looking some like this. where do we put this script? in the first scene?

    Comment by dizitalART — March 13, 2008 @ 7:58 pm

  2. Hi,

    Yes. Make the first scene a blank screen, and put this code in one of the frames.

    Then have the end of each of your scenes redirect back to the beginning of the first scene.

    Comment by Ashton Sanders — March 14, 2008 @ 4:14 pm

  3. Hi Ashton,

    I added the code to the end of each scene (3 scenes total) where I previously had code to go to the next scene. But this seems to repeat quite frequently. Do you know a way to prevent repeating schne?

    Comment by Jeff — April 6, 2009 @ 9:17 am

  4. Hi Jeff,

    Thanks for the question. Yes, to make the scenes not repeat, do not put the current scene in the randomizing code.

    For example, for the first scene, don’t have “GotoAndPlay(“scenename”, 1);” You’ll have to change the code to be for two scenes since you are removing one of them. That way, there is no chance to play the scene you are already in.

    I hope that helps,
    Ashton Sanders

    Comment by Ashton.Sanders — April 6, 2009 @ 10:06 am

  5. Niiiiiiice! Exactly what I was looking for. Using with GotoAndStop to randomly display different call-to-action hero images on a home page. THX :-)

    Comment by OmegaTech — August 8, 2010 @ 5:38 am

  6. Sure this could be use for what I’m currently working on – starts with cow wandering around a field for ten seconds, and it should then randomly select one of 11 scenes, each one with a cow pat in a different bit of the field – once the cow pat scene had finished, it then goes back to the cow wandering the field – all working fine apart from the randomly selecting which cow pat scene it plays – I’ve named all the frame1s for each scene with scene 1, scene 2 etc because a different tutorial told me too, but I still can’t get it to work… here’s the script that I’ve currently got in the last frame of the cow wandering about the field scene…

    set(scenes, ["scene 2", "scene 3", "scene 4", "scene 5", "scene 6", "scene 7", "scene 8", "scene 9", "scene 10", "scene 11", "scene 12"]);
    gotoAndPlay(scenes[random(12)]);

    (on the first frame of the cow wandering about the field scene is the random movement script that makes the wander randomly)

    the whole project is for a school fete, where my form want to run a poo-lette stall – people “bet” on which bit of the field the cow will poo in first – figured we can’t get a real cow into school so this will have to do – anyone who can help I’d be very grateful.

    Comment by Olzki — June 21, 2011 @ 12:30 pm

  7. TYVM! Exactly what I was looking for (like OmegaTech said).
    Needed that to randomly show images on my website banner (school project)

    Thanks very much!

    Comment by Arthur — October 11, 2011 @ 5:00 pm

  8. U can try this code…
    Condition = for 3 scene (name scene = Scene_1,Scene_2,Scene_3)
    Where to put this code (First layer, first frame)

    sheeta = random(3);
    if (sheeta == 0) {
    gotoAndStop(“Scene_1″, 1);
    }
    if (sheeta == 1) {
    gotoAndStop(“Scene_2″, 1);
    }
    if (sheeta == 2) {
    gotoAndStop(“Scene_3″, 1);
    }

    u can use button for the trigger to execute this action with the code :
    on(release)
    {

    sheeta = random(3);
    if (sheeta == 0) {
    gotoAndStop(“Scene_1″, 1);
    }
    if (sheeta == 1) {
    gotoAndStop(“Scene_2″, 1);
    }
    if (sheeta == 2) {
    gotoAndStop(“Scene_3″, 1);
    }

    }

    good luck

    Comment by Wahyu-Indonesia — July 15, 2012 @ 4:21 am

RSS feed for comments on this post. TrackBack URL

Leave a comment