NOTICE: ActionScipt No Longer Supported

These blog posts are for ActionScript, which is a programming language for Adobe Flash Player. Unfortunately, Adobe Flash is no longer supported for web applications, so this information is likely not very useful to most developers. We are keeping it for those who still enjoy ActionScript and for posterity. We named our company after it, after all.

ActionScript

Flash Random Scene ActionScript

8 Comments

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

8 Comments

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

  • 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.

  • 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?

  • 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

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

  • 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.

  • 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!

  • 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

Leave a Reply to Ashton Sanders Cancel reply

Your email address will not be published. Required fields are marked *