# \[FMX\] Loops & Math.random, help with short simple code

**URL:** <https://forum.kirupa.com/t/fmx-loops-math-random-help-with-short-simple-code/26418>\
**Category:** Uncategorized\
**Created:** [July 22, 2003, 10:32pm UTC](https://forum.kirupa.com/t/fmx-loops-math-random-help-with-short-simple-code/26418 "2003-07-22T22:32:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ranoka](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ranoka/32/265_2.png) [@Ranoka](https://forum.kirupa.com/u/Ranoka)\
**Post date:** [July 22, 2003, 10:32pm UTC](https://forum.kirupa.com/t/fmx-loops-math-random-help-with-short-simple-code/26418/1 "2003-07-22T22:32:21Z")

</div>

Hi everyone,  
I’m only just learning Actionscript, and have come up with a piece of code that I want to get working.

On my main timeline I have a movieclip with an instance called “eye”. What I would like to happen if for a random number to be generated and assigned to “blink”. If blink = 20 then I want to play "eye"s animation. This is the code I have so far:

[AS]  
blink=Math.random(20)  
if(blink=20){  
\_root.eye.gotoAndPlay(2)  
}  
[/AS]

I’ve told the eye animation to stop() in the first frame to stop it from animating before being told.

I know this won’t work yet, I haven’t worked out how to put this code in a loop to keep generating a new value for blink, and test if it is 20.

Any help with finishing my code will great! :beam:

Thanks in advance!

---

<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:** [July 22, 2003, 10:45pm UTC](https://forum.kirupa.com/t/fmx-loops-math-random-help-with-short-simple-code/26418/2 "2003-07-22T22:45:38Z")

</div>

You could use an onEnterFrame handler…  
[AS]eye.onEnterFrame = function() {  
var blink = Math.floor(Math.random()\*20);  
if (!blink) {  
this.play();  
}  
};[/AS]  
😉

---

<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:** [July 23, 2003, 12:37am UTC](https://forum.kirupa.com/t/fmx-loops-math-random-help-with-short-simple-code/26418/3 "2003-07-23T00:37:31Z")

</div>

It does not whant to work 'cause if you want to compare 2 values, you need to use a double = sign: if(blink==20)

The fastest way to do that is to put this code _on_ your mc. It’s a little easier and faster thatn kode’s code:

```auto
onClipEvent(enterFrame){
if(!random(20)) play();
}

```

---

<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:** [July 23, 2003, 3:43am UTC](https://forum.kirupa.com/t/fmx-loops-math-random-help-with-short-simple-code/26418/4 "2003-07-23T03:43:36Z")

</div>

and also a little more deprecated 🙂

---

<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:** [July 23, 2003, 5:25am UTC](https://forum.kirupa.com/t/fmx-loops-math-random-help-with-short-simple-code/26418/5 "2003-07-23T05:25:18Z")

</div>

> _Originally posted by brainy_  
> **and also a little more deprecated 🙂**

That’s what I was going to say… 😛

---

<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:** [July 24, 2003, 7:25pm UTC](https://forum.kirupa.com/t/fmx-loops-math-random-help-with-short-simple-code/26418/6 "2003-07-24T19:25:20Z")

</div>

Thanks for the replies!

I thought to myself during the day it would probably make more sence to place the actionscript on the MC.

What would the up to date code of [M]'s code be? I’m trying to learn actionscript so an explanation of whats happening in the code would be very helpful! (I avoid cutting and pasting code, wont help me learn.)

Thanks 🙂

---

<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:** [July 28, 2003, 3:24pm UTC](https://forum.kirupa.com/t/fmx-loops-math-random-help-with-short-simple-code/26418/7 "2003-07-28T15:24:40Z")

</div>

> \*Originally posted by [m] \*  
> \*\*The fastest way to do that is to put this code _on_ your mc. It’s a little easier and faster thatn kode’s code:

```auto
onClipEvent(enterFrame){
if(!random(20)) play();
}

```

\*\*

I tried using this code in the movie clip but it told me I can only use the onClipEvent on a movie clip instance.  
I put this code in a movie clip symbol.  
What am I doing wrong?

---

<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:** [July 28, 2003, 3:36pm UTC](https://forum.kirupa.com/t/fmx-loops-math-random-help-with-short-simple-code/26418/8 "2003-07-28T15:36:48Z")

</div>

No worries, I worked it out

Rather then putting the code in the movie clip I have to put the code on the instance.

The code does the job, thanks [M]!
