Skip to main content
Participant
February 23, 2017
Answered

Random Frame

  • February 23, 2017
  • 6 replies
  • 3886 views

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

    This topic has been closed for replies.
    Correct answer AMULI

    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);

    }

    6 replies

    Participant
    February 24, 2017

    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

    AMULI
    Inspiring
    February 24, 2017

    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 ?

    AMULI
    Inspiring
    February 24, 2017

    A downloadable example of playing the different frames/states of a timeline in random order here :

    https://forums.adobe.com/message/9309084#9309084

    Legend
    February 23, 2017
    Colin Holgate
    Inspiring
    February 23, 2017

    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.

    Participant
    February 23, 2017

    Animate CC

    I have just started and I haven't any information about coding

    Szalam
    Community Expert
    Community Expert
    February 23, 2017

    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.

    Szalam
    Community Expert
    Community Expert
    February 23, 2017

    What software are you using?