views:

1040

answers:

2

In an iPhone application I'm developing I have a Image View that displays eyes and I want to make them blink, an obviously blinking is a random thing, it can't be routine and keep repeating like a UIImageView Animation would do on repeat.

How would I set up an animation that displays one frame (blinked eyes) then sets back to the original image and does this in random intervals?

Remember when answering I'm a newbie.

A: 

You could use a NSTimer. Have the timer's delegate method change the eye image to the blink image, and then set the timer's fire date to a random time.
Once the NSTimer is fired, use this code to set it to a random time:
[yourTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:rand()%maxTimeBetweenBlinks]];
replace yourTimer and maxTimeBetweenBlinks with the right values, of course
You would probably want to use srand(time(NULL)) to seed the random number generator

nazgul42
I see, do you know what specifically you would type to set the timers fire date to a random time?
Thanks, you have really helped me.
+1  A: 

cocos2d has blink action you can "schedule" the action on image sprite at random time rnd()%10 .

Surya