Goto And Play random frame?

Ok. I have a 30-frames mc,each frame has a logo of my client. I want visitors see 30 logos randomly and each logo will pause 2 seconds for viewing. How can I do it?
any help would be appreciated.

well… one way of doing it is:

onClipEvent(enterFrame){
i++;
if(i==(fps*2){
this.gotoAndStop(Math.round(Math.random()*30));
i=0;
}
}

the “fps*2”, is just your current fps times 2, insert a number instead. :slight_smile:
This is not the best way to do it, cuz if the viewer has a slow pc, it’d take longer to count the i. so they’d have to wait maybe 4-5 seconds… :-\

but a better way would be to set up intervals, can’t remember how…

http://www.kirupaforum.com/showthread.php?s=&threadid=7457

how about change in this thread
http://www.kirupaforum.com/showthre…=&threadid=7457

those lines:

One good solution is to write the title like that:**
[FMX]Arg!! Desperate!! need help please!!
[F5]Arg!! For Actionscript Guru!! Beginner needs help!**

to:

One good solution is to write the title with a short description of your problem preceeded by the Flash Version between brackets, like this:**
[FMX]goto And Play random frame ??
[F5]Random Generator**

That way the helper will have a preview of what is inside the thread, know what I mean?:wink:

What do ya think?

[EDIT]Typos

hahahaha thats the first thing I thought when I first read ilyaslamasse’s post originally when I first found this forum. It gave me quite a giggle :slight_smile:

btw, as for the question, if using MX, setInterval may be the way to go. And if you dont want them to repeat, make an array of frames and take randomly from that until gone


// logo clip
logoClip = _root.presentation.logos;

// array of frames
frames = [];
for(i = 0; i < logoClip._totalframes; i++) frames* = i;

// function to go to a random frame (logo)
nextLogo = function(){
	if (len = frames.length) logoClip.gotoAndStop(frames.splice(random(len),1));
	else whenDone();
}

// called when all frames are played
whenDone = function(){
	trace("done");
	clearInterval(logoTimer);
}
// call once to start
nextLogo();
// set interval to nextLogo every 2 seconds
logoTimer = setInterval(nextLogo, 2000);

[I use FMX]Thank you very muchhhhhh , senocular!!!
you save my life. I tried many ways I could do until I almost reached my goal but I found that the first logo weren’t shown randomly but always first frame of logoClip instead. I can not figure out why it was.

this is my code at 1st frame of main timeline:
nums = “”;
k=1;
nofNums = 30;
range = 30;
numsSoFar = 0;
showNums = new Array();
while (numsSoFar<nofNums) {
aNum = parseInt(Math.random()*range);
if (nums.indexOf("@"+aNum+"@") ==-1) {
nums +="@"+aNum+"@";
showNums[numsSoFar++] = aNum;
}
}

second frame:
// I place the logoClip here and put this on it
onClipEvent(load){
this.gotoAndStop(showNums[0]);
}

20th frame:
mc.gotoAndStop(showNums[k++]);
gotoAndPlay(2);

anyway,Thank you very much! though I still don’t understand some parts of your code but I will study it again when I finish my work.

i usually just do…
gotoAndPlay(random(_totalframes)+1);
…in one of the frames.

random was deprecated with MX, it would now be…

gotoAndPlay(1+Math.random()*_totalframes);

Also… this code is too short… it is not accepted here!!! :scream:

LOL, just kidding man, this is the same method I use for random frames too.

*Originally posted by lostinbeta *
**random was deprecated with MX, it would now be…

gotoAndPlay(1+Math.random()*_totalframes);
**

random was deprecated with 5 and gotoAndPlay will not always work with non integer floats :wink:

cough
btw, random() is much much faster and still works, so use it
cough

*Originally posted by senocular *
coughbtw, random() is much much faster and still works, so use it cough **
coughI have to agree with that
cough

And Guig0, I know, it was ironic. :beam:

BAH, if it is faster than why was it deprecated!?!?!?

Man, I always learn the slow way :wink:

And I didn’t mean to say <B>with</B> MX, I meant to say <B>in</B> MX, I have no idea what is deprecated and what isn’t in Flash 5. (in my defense ;))

just to be more ECMA-262 compliant :-\

and you meant is and not was too, right? :beam:

I don’t what are the criteria for deprecation (???) but I’ve always heard that the older the code, the faster. [SIZE=1]Well, maybe not Flash 2 code…[/SIZE]

*Originally posted by ilyaslamasse *
**I don’t what are the criteria for deprecation (???) but I’ve always heard that the older the code, the faster. [SIZE=1]Well, maybe not Flash 2 code…[/SIZE] **

yeah, thats true for everything. The Flash 5+ syntax you are used to is mostly (all new syntax that is… new methods and classes etc) written in Flash actionscript internally, a lot of times just wrappers of Flash 4 methods. This being the case, its pretty obvious to assume Flash 4 methods are infact faster.

:scream:

Well that sucks, I guess I gotta go back and learn all the Flash 4 code now :angry: :bad:

Chances are you’ll never notice a difference in performance. Only really script heavy projects would benefit from it. On a regular-use basis, its nothing to worry about. Even then the main cause of slowdown if any is simple visual frame refresh. :slight_smile:

Well if ever I get good enough at actionscript to do some heavier stuff (haha, I kid myself so) I now know that old syntax is faster… that is a good thing to know.

Well, you also have to consider the amount of code you have to use. random may be faster than Math.random, but this is just a function to function comparison. If you’re involved in “heavier stuff”, well, you just can’t get heavy with Flash 4 :stuck_out_tongue:

yeah, have you guys ever tried the Math.sqrt() function to find a square root of a function?
i once had a movie that used the distance formula on each enterFrame and it was killing my CPU. i traced it all the way back to that function.

Yep, true, that’s why I now compare squared distances. It proves really faster :slight_smile: