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

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!

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

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:

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

and also a little more deprecated :slight_smile:

Originally posted by brainy
and also a little more deprecated :slight_smile:

That’s what I was going to say… :stuck_out_tongue:

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 :slight_smile:

*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:

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?

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