Copy link to clipboard
Copied
I have identified a button.
This button was going to the next frame.
But I want a random frame.
Would you like to see sample code.
thank you so much
Say your button has instance name randomFrameBtn, add this code (adapt the minFrameNb and maxFrameNb to your timeline) :
randomFrameBtn.addEventListener( MouseEvent.CLICK, goToRandomFrame);
function goToRandomFrame( event:MouseEvent): void
{
var minFrameNb = 5;
var maxFrameNb = 20;
var randomFrameNb = minFrameNb +Math.floor( Math.random() * (maxFrameNb - minFrameNb + 1));
gotoAndStop( randomFrameNb);
}
Copy link to clipboard
Copied
What software are you using?
Copy link to clipboard
Copied
Animate CC
I have just started and I haven't any information about coding
Copy link to clipboard
Copied
I'm going to move your post over to the Animate forum. The Coding Corner forum is more for general coding stuff whereas the Animate forum folk will be able to offer you more specific help for Animate.
Copy link to clipboard
Copied
Code to go to the next frame might look like this:
this.gotoAndStop(this.currentFrame+1);
Code to go a frame that is up to 10 frames ahead of where you are now would look like this:
this.gotoAndStop(this.currentFrame+Math.random()*10);
If you wanted to jump to a random frame starting at 15 and ending at 25, it would be like this:
this.gotoAndStop(15+Math.random()*10);
Math.random() gives you a value between 0 and almost 1. Multiplying that by 10 would make the number be from 0 to almost 10.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
A downloadable example of playing the different frames/states of a timeline in random order here :
Copy link to clipboard
Copied
Thank you, AMULI but I am so bad in coding that I do not understand most of the writings.
I will explain my problem;
There were sample codes in Animate CC
Random number generation code;
function fl_GenerateRandomNumber_4(limit:Number):Number
{ var randomNumber:Number = Math.floor(Math.random()*(limit+1));
return randomNumber;
}
trace(fl_GenerateRandomNumber_4(100));
AND
Next frame pass code;
newnumber.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextFrame);
function fl_ClickToGoToNextFrame(event:MouseEvent):void
{ nextFrame(); }
My question is;
How do I add this code any button?
For example, I have a button which name is "newnumber"
and I want, when I click this button, it goes to random frame
Copy link to clipboard
Copied
emrek16464832 a écrit
when I click this button, it goes to random frame
I guess, it's not totally random ! Which range of frames ? Said differently, what are the minimum and maximum frame number your random computation should return ?
Copy link to clipboard
Copied
Say your button has instance name randomFrameBtn, add this code (adapt the minFrameNb and maxFrameNb to your timeline) :
randomFrameBtn.addEventListener( MouseEvent.CLICK, goToRandomFrame);
function goToRandomFrame( event:MouseEvent): void
{
var minFrameNb = 5;
var maxFrameNb = 20;
var randomFrameNb = minFrameNb +Math.floor( Math.random() * (maxFrameNb - minFrameNb + 1));
gotoAndStop( randomFrameNb);
}
Copy link to clipboard
Copied
Thank you very very very much .
It works like a machine
Copy link to clipboard
Copied
You're welcome, emrek16464832
Copy link to clipboard
Copied
I'm looking for almost exactly the same technique (just don't want to have to click anything for the function to work — want it to happen on load/refresh). That said, I'm simply trying to get this example to work, but to no avail. When I preview, I see nothing (but I should be seeing my button, at least, or numbers if it's working). Only way I see anything (which consists of the timeline simply looping through) is when I start to modify the code a bit to more closely match other examples I have. Couple questions:
1. Does this code work for an HTML5 canvas file?
2. Is there a working file I can reference?
3. How would one modify this to randomly select a frame upon load/refresh (no need to click a button)?
Really appreciate any insight/help. I think I'm close, and yet so far away!
Copy link to clipboard
Copied
What I said in message 4 would work from Canvas, and doesn't need a click:
this.gotoAndStop(15+Math.random()*10);
or:
this.gotoAndPlay(15+Math.random()*10);
if you want it to play from there. In my example it would jump to a random frame in the range of 15 to 24. Change the start frame number and the number of different frames, to match your case.
One thing to be aware of, if you are going to play sound it will fail. In most browsers HTML5 requires a user action before it allows sound to play.
Copy link to clipboard
Copied
So easy … thank you! Works just as expected. My apologies for not inferring the code in message 4 to work for my needs.
Copy link to clipboard
Copied
Hello Colin_Holgate
Thank you for your knowledge. I do have a question if you may. I also am working on an HTML5 project. I would like my project to jump randomly to specific frames for example frame 5, 10 and 15 in a random order onload how do I achieve this?
Kind regards
Copy link to clipboard
Copied
Something else, if you don't see your button you should look at the developer Console, to see what error message it's giving you.