Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Nov 07, 2018 Nov 07, 2018

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!

1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 07, 2018 Nov 07, 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 <S

...
Translate
Community Expert ,
Nov 07, 2018 Nov 07, 2018
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 07, 2018 Nov 07, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 07, 2018 Nov 07, 2018
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines