Skip to main content
Participant
January 26, 2013
Answered

Play random predefined set of frames

  • January 26, 2013
  • 1 reply
  • 2927 views

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

This topic has been closed for replies.
Correct answer kglad

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


:

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

}

1 reply

freetLAuthor
Participant
January 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

kglad
Community Expert
Community Expert
January 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.

freetLAuthor
Participant
January 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