Copy link to clipboard
Copied
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!
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
Copy link to clipboard
Copied
See if this tutorial helps you. Adobe Animate CC - Scene Navigation Using ActionScript 3 - YouTube
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
That all being said, scenes are about the worst way to do something like this. There's a reason movieclips exist.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now