Skip to main content
Participant
November 7, 2018
Answered

Is there a way to call a random scene in Adobe Animate?

  • November 7, 2018
  • 3 replies
  • 1319 views

I was wondering if it was possible using actionscript or through some other means to call a random scene from a selection of possibilities.

I want to make an animation with user interactivity in which upon clicking a button one of several outcomes can occur. As a very simple example: if the animation were flipping a coin, there would need to be a possibility of randomly landing on heads or tails.

I've made each of the outcomes and put them in different scenes, but that's as far as I've got, and I don't even know if this is the right way to go about it.

Any advice on how to do this would be great.

Many thanks!

This topic has been closed for replies.
Correct answer JoãoCésar17023019

Hi.

Adding to Nick's suggestion, here is a sample:

AS3 code:

First scene:

import flash.display.SimpleButton;

import flash.events.MouseEvent;

// this check makes sure that  everytime the user navigates to this frame the variables won't be reset

if (!this.hasStarted)

{

    var allowRepetition:Boolean = false; // set to true or false to allow repetition. If it is set to false, then the code will pick all the scenes in the array before picking the same scene again

    var randomScenes:Vector.<String> = new <String> // put the name of your scenes here

    [

          "Scene 1",

          "Scene 2",

          "Scene 3",

          "Scene 4",

          "Scene 5"

    ];

    var count:uint = 0;

    this.hasStarted = true;

}

function clickHandler(e:MouseEvent):void

{

    if (allowRepetition)

          gotoAndStop(1, randomScenes[uint(Math.random() * randomScenes.length)]); // if allowRepetion is set to false, then the code will pick a totally random scene

    else

    {

          if (count % randomScenes.length == 0) // everytime all the scenes are picked, this block of code will randomize the array again

          {

               randomScenes = randomScenes.sort(function(a:*, b:*):int

               {

                   return ( Math.random() > 0.5 ) ? 1 : -1;

               });

          }

          gotoAndStop(1, randomScenes[++count % randomScenes.length]); // incrementally picks a scene as the array is already sorted

    }

}

function setButton(button:SimpleButton):void

{

    // Check if the button already has a listener to not add multiple listeneres

    if (!button.hasEventListener(MouseEvent.CLICK))

          button.addEventListener(MouseEvent.CLICK, clickHandler);

}

stop();

setButton(sceneButton);

Second scene:

stop();

setButton(sceneButton1);

Third scene:

stop();

setButton(sceneButton2);

Fourth scene:

stop();

setButton(sceneButton3);

Fifth scene:

stop();

setButton(sceneButton4);

FLA download:

animate_cc_as3_random_scenes.zip - Google Drive

Regards,

JC

3 replies

Legend
November 7, 2018

That all being said, scenes are about the worst way to do something like this. There's a reason movieclips exist.

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
November 7, 2018

Hi.

Adding to Nick's suggestion, here is a sample:

AS3 code:

First scene:

import flash.display.SimpleButton;

import flash.events.MouseEvent;

// this check makes sure that  everytime the user navigates to this frame the variables won't be reset

if (!this.hasStarted)

{

    var allowRepetition:Boolean = false; // set to true or false to allow repetition. If it is set to false, then the code will pick all the scenes in the array before picking the same scene again

    var randomScenes:Vector.<String> = new <String> // put the name of your scenes here

    [

          "Scene 1",

          "Scene 2",

          "Scene 3",

          "Scene 4",

          "Scene 5"

    ];

    var count:uint = 0;

    this.hasStarted = true;

}

function clickHandler(e:MouseEvent):void

{

    if (allowRepetition)

          gotoAndStop(1, randomScenes[uint(Math.random() * randomScenes.length)]); // if allowRepetion is set to false, then the code will pick a totally random scene

    else

    {

          if (count % randomScenes.length == 0) // everytime all the scenes are picked, this block of code will randomize the array again

          {

               randomScenes = randomScenes.sort(function(a:*, b:*):int

               {

                   return ( Math.random() > 0.5 ) ? 1 : -1;

               });

          }

          gotoAndStop(1, randomScenes[++count % randomScenes.length]); // incrementally picks a scene as the array is already sorted

    }

}

function setButton(button:SimpleButton):void

{

    // Check if the button already has a listener to not add multiple listeneres

    if (!button.hasEventListener(MouseEvent.CLICK))

          button.addEventListener(MouseEvent.CLICK, clickHandler);

}

stop();

setButton(sceneButton);

Second scene:

stop();

setButton(sceneButton1);

Third scene:

stop();

setButton(sceneButton2);

Fourth scene:

stop();

setButton(sceneButton3);

Fifth scene:

stop();

setButton(sceneButton4);

FLA download:

animate_cc_as3_random_scenes.zip - Google Drive

Regards,

JC

Community Expert
November 7, 2018