Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • EspaƱol
      • FranƧais
      • PortuguĆŖs
  • ę—„ęœ¬čŖžć‚³ćƒŸćƒ„ćƒ‹ćƒ†ć‚£
  • ķ•œźµ­ ģ»¤ė®¤ė‹ˆķ‹°
0

Play random predefined set of frames

New Here ,
Jan 26, 2013 Jan 26, 2013

hi

i am pretty new to AS3 so i need a bit of help from you guys. I want to randomize the  results when i press a button, right now i have this code. i have 5 Images. Each is set on a frame where only this image is visible. So i am randomizing the frames when i click on the button. Each time it gives me a random frame number and shows me the image on that frame. Here is the code (made this with a tutorial)

stop();

random_btn.addEventListener(MouseEvent.CLICK, choose);

function choose(event:MouseEvent):void{

          var pic_number : Number = 6;

          var randomFrame:Number = Math.ceil(Math.random() * pic_number);

          trace(randomFrame);

          gotoAndStop(randomFrame);

}

Right now i want to do this more advanced, i want to animate in the image. for example i have 5 animations, the first animation goes from frame 2 till 8, the second goes from 9 to 12 and etc, so i got a set of frames (2to8)(9to12) and not like in the previos where i just got one frame. Whats the best way to accomplish it, so pressing the button gives me each time a diffrent animation, it animates from start and then stops. I hope you can help me, thx

TOPICS
ActionScript
2.8K
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 , Jan 26, 2013 Jan 26, 2013

:

stop();  // and put stops on each frame where you want the timeline to stop

var frameA:Array=[1,11,12,13,14,15,16,17];

random_btn.addEventListener(MouseEvent.CLICK, choose);

function choose(event:MouseEvent):void{

          var randomFrame:Number = frameA[Math.floor(Math.random() * frameA.length)];

          trace(randomFrame);

          gotoAndPlay(randomFrame);

}

Translate
New Here ,
Jan 26, 2013 Jan 26, 2013

i wanted to add also, that this should be for a small game, so the user pushs the button and gets a random pice. I would have 9 prices for example, and each price have a diffrent animation. thats the plan i am open for any kind of suggestion how to make this, thx

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 ,
Jan 26, 2013 Jan 26, 2013

add the initial keyframe numbers to an array and select one of the array elements at random.  put a stop() on the last frame of each animation.

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
New Here ,
Jan 26, 2013 Jan 26, 2013

thx for your answer. I am really new to flash, so i dont understand you that much , can you paste me a short example of the code you mentiond. I am right now trying to make a intro that plays from frame 1 till frame 10, and then on frames 11 12 13 14 15 16 17 i would playe movie clips on each one( for example movie clip 1 on frame 11 as a keyframe and on the rest of the keyframes blank so it dosent appear there and i would add stop on the end of the frame in the movie so it stops and dosent repeat). Thx again for your reply,

best regards

freeTl

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 ,
Jan 26, 2013 Jan 26, 2013

:

stop();  // and put stops on each frame where you want the timeline to stop

var frameA:Array=[1,11,12,13,14,15,16,17];

random_btn.addEventListener(MouseEvent.CLICK, choose);

function choose(event:MouseEvent):void{

          var randomFrame:Number = frameA[Math.floor(Math.random() * frameA.length)];

          trace(randomFrame);

          gotoAndPlay(randomFrame);

}

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
New Here ,
Jan 27, 2013 Jan 27, 2013

thx , the code is good,

one last question, can i add a code that would check the last result with the current one , if the same that another result comes, basiclly what i mean that the results not repeat each another, because i get sometimes for examples, the frames 12 12 12 in a row by hiting the button. thx in advance.

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 ,
Jan 27, 2013 Jan 27, 2013
LATEST

use:

stop();  // and put stops on each frame where you want the timeline to stop

var frameA:Array=[1,11,12,13,14,15,16,17];

shuffle(frameA);

var index:int=0

random_btn.addEventListener(MouseEvent.CLICK, choose);

function choose(event:MouseEvent):void{

          var randomFrame:Number = frameA[index]

          trace(randomFrame);

          gotoAndPlay(randomFrame);

index=(index+1)%frameA.length;

}

function shuffle(a:Array) {

    var p:int;

    var t:*;

    var ivar:int;

    for (ivar = a.length-1; ivar>=0; ivar--) {

        p=Math.floor((ivar+1)*Math.random());

        t = a[ivar];

        a[ivar] = a

;

        a

= t;

    }

}

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