|
-
Random Eye Blinking
Hello all,
A novice coder here with a, hopefully valid, question. I have a character with blinking eyes. In order to make the eye blinking look natural I wanted each interval between blinks to be random. Unfortunately my code generates one random number and applies it to all the intervals. Is there something I can add to this code to make a truly random # for each interval?
Any help would be greatly appreciated! - B
---------------------------------------
function blink() {
evilblink.gotoAndPlay("blink");
}
function Blinkorama() {
setInterval(blink, random(3000)+1000);
}
Blinkorama();
-
-----------------------------------------------------
code:
blinkTime = random(3000)+1000;//should initialize in the function but just in case it doesnt
function blink() {
blinkTime = random(3000)+1000;//set time to wait for the interval
evilblink.gotoAndPlay("blink");//does the blinking stuff
clearInterval(blinker);
//clears the interval so a new amount of time can be used
Blinkorama();//resets the interval with the new time
}
function Blinkorama() {
blinker = setInterval(blink,blinkTime);
//blinker is the interval name, blink is the function to be called
//blinkTime is of course the amount of time till it is called
}
Blinkorama();
---------------------------------------------------------
not really clean but it will do what you need and it doesn't change your original code too much so you should be able to maintin understanding of all the working parts.
Good luck,
Shawn
Last edited by paradox244; 11-09-2003 at 12:58 AM.
-
danke Shawn,
I did try the clearInterval at one time but couldn't get it to work. Guess you have to name the timer eh!
Thank you so much
- B
-
Hi, an even a more novice coder here,
the setInterval() function is a great but I am a little fuzzy on why and what purpose the clearInterval() funtion does.
The reference panel doesn't have much on this. thanx!
-
I hope I'm not misinforming you with my own view. To me it's like a box. blinkTime = random(3000)+1000; puts a result in the box. To get a different result in there you need to first empty (clearInterval)the box. Only one result can fit in the box at a given time. Perhaps one of the pros here could give us both an answer that is more accurate?
- B
-
Is there any chance you can post the fla with the blinking and the code...I have been trying to make a blinking effect that looks realistic....I would love to see how it's done
-
A setInterval() is like one of those round 24hr timers you plug your lamp into. It runs for the amount of time you have set and it triggers the action, then it runs around again and triggers the action, and so on, and so on and so on. It never stops. That's what the clearInterval action is for. It stops the setInterval.
-
Okay I commented the code for you guys guess I should have done that in the first place.
Anyway to answer the questions I caused,
The lamp timer analogy is perfect DallasNYC thank you for answering that one.
clearInterval tells flash to quit using the interval. Basically in this case we want to change the amount of time between blinks everytime the blinking happens, so to do this we have to reset the timer everytime the lamp comes on, thus the clearInterval.
In any other case clearInterval is used to tell flash to quit repeating an action, it saves CPU cycles, especially if whatever was affected is no longer visible. Say we used setInterval and had a function that moved something to the right each time it was called, well when the object goes off the right side of the stage we don't need to have flash move it anymore, so we tell it to stop with clearInterval.
I hope that clarifys its use a little bit
And like I said before the code isn't necessarily the best but it works.
If you have more questions email me at [email protected]
Good Luck,
Shawn
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|