# Goto And Play random frame?

**URL:** <https://forum.kirupa.com/t/goto-and-play-random-frame/10785>\
**Category:** Uncategorized\
**Created:** [January 7, 2003, 10:04am UTC](https://forum.kirupa.com/t/goto-and-play-random-frame/10785 "2003-01-07T10:04:32Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jepezi](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/jepezi/32/57_2.png) [@jepezi](https://forum.kirupa.com/u/jepezi)\
**Post date:** [January 7, 2003, 10:04am UTC](https://forum.kirupa.com/t/goto-and-play-random-frame/10785/1 "2003-01-07T10:04:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 7, 2003, 11:20am UTC](https://forum.kirupa.com/t/goto-and-play-random-frame/10785/2 "2003-01-07T11:20:36Z")

</div>

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. 🙂  
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…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 7, 2003, 3:08pm UTC](https://forum.kirupa.com/t/goto-and-play-random-frame/10785/3 "2003-01-07T15:08:04Z")

</div>

[http://www.kirupaforum.com/showthread.php?s=&threadid=7457](http://www.kirupaforum.com/showthread.php?s=&threadid=7457)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 7, 2003, 3:27pm UTC](https://forum.kirupa.com/t/goto-and-play-random-frame/10785/4 "2003-01-07T15:27:24Z")

</div>

how about change in this thread  
[http://www.kirupaforum.com/showthre…=&threadid=7457](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?😉

What do ya think?

[EDIT]Typos

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 7, 2003, 4:32pm UTC](https://forum.kirupa.com/t/goto-and-play-random-frame/10785/5 "2003-01-07T16:32:27Z")

</div>

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 🙂

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

```auto

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

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 8, 2003, 2:39am UTC](https://forum.kirupa.com/t/goto-and-play-random-frame/10785/6 "2003-01-08T02:39:11Z")

</div>

[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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 8, 2003, 5:28am UTC](https://forum.kirupa.com/t/goto-and-play-random-frame/10785/7 "2003-01-08T05:28:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 8, 2003, 5:38am UTC](https://forum.kirupa.com/t/goto-and-play-random-frame/10785/8 "2003-01-08T05:38:17Z")

</div>

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!!! 😱

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 8, 2003, 12:21pm UTC](https://forum.kirupa.com/t/goto-and-play-random-frame/10785/9 "2003-01-08T12:21:25Z")

</div>

> \*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 😉

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 8, 2003, 2:19pm UTC](https://forum.kirupa.com/t/goto-and-play-random-frame/10785/10 "2003-01-08T14:19:31Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 8, 2003, 5:46pm UTC](https://forum.kirupa.com/t/goto-and-play-random-frame/10785/11 "2003-01-08T17:46:57Z")

</div>

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

Man, I always learn the slow way 😉

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 8, 2003, 5:50pm UTC](https://forum.kirupa.com/t/goto-and-play-random-frame/10785/12 "2003-01-08T17:50:44Z")

</div>

just to be more ECMA-262 compliant :-\

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 8, 2003, 5:51pm UTC](https://forum.kirupa.com/t/goto-and-play-random-frame/10785/13 "2003-01-08T17:51:35Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 8, 2003, 5:58pm UTC](https://forum.kirupa.com/t/goto-and-play-random-frame/10785/14 "2003-01-08T17:58:46Z")

</div>

> \*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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 8, 2003, 6:07pm UTC](https://forum.kirupa.com/t/goto-and-play-random-frame/10785/15 "2003-01-08T18:07:51Z")

</div>

😱

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 8, 2003, 6:34pm UTC](https://forum.kirupa.com/t/goto-and-play-random-frame/10785/16 "2003-01-08T18:34:03Z")

</div>

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. 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 8, 2003, 6:44pm UTC](https://forum.kirupa.com/t/goto-and-play-random-frame/10785/17 "2003-01-08T18:44:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 9, 2003, 1:38am UTC](https://forum.kirupa.com/t/goto-and-play-random-frame/10785/18 "2003-01-09T01:38:17Z")

</div>

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 😛

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 9, 2003, 5:59am UTC](https://forum.kirupa.com/t/goto-and-play-random-frame/10785/19 "2003-01-09T05:59:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 9, 2003, 11:04am UTC](https://forum.kirupa.com/t/goto-and-play-random-frame/10785/20 "2003-01-09T11:04:47Z")

</div>

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

[Next page](https://forum.kirupa.com/t/goto-and-play-random-frame/10785.md?page=2)
