Copy link to clipboard
Copied
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
:
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);
}
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
:
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);
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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;
}
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now