// // EyeBlink.m // PopFizz // // Created by Juraj Hlavac on 12/18/09. // Copyright 2009 __MyCompanyName__. All rights reserved. // #import "EyeBlink.h" @implementation EyeBlink +(id) actionWithOpenDuration:(ccTime)oD openVar:(ccTime)oV closedDuration:(ccTime)cD closedVar:(ccTime)cV openFrame:(CCSpriteFrame*)oF closedFrame:(CCSpriteFrame*)cF repeatForever:(bool)rF { return [[[self alloc] initWithOpenDuration:oD openVar:oV closedDuration:cD closedVar:cV openFrame:oF closedFrame:cF repeatForever:rF] autorelease]; } -(id) initWithOpenDuration:(ccTime)oD openVar:(ccTime)oV closedDuration:(ccTime)cD closedVar:(ccTime)cV openFrame:(CCSpriteFrame*)oF closedFrame:(CCSpriteFrame*)cF repeatForever:(bool)rF { if((self=[super initWithDuration:(oD + cD)])) { _openDuration = oD; _openVar = oV; _closedDuration = cD; _closedVar = cV; _openFrame = oF; _closedFrame = cF; _repeatForever = rF; } return self; } -(id) copyWithZone:(NSZone*)zone { CCAction *copy = [[[self class] allocWithZone:zone] initWithOpenDuration:_openDuration openVar:_openVar closedDuration:_closedDuration closedVar:_closedVar openFrame:_openFrame closedFrame:_closedFrame repeatForever:_repeatForever]; return copy; } -(void) startWithTarget:(id)aTarget { [super startWithTarget:aTarget]; _actualOpenDuration = MAX(0.000001f, _openDuration + CCRANDOM_MINUS1_1() * _openVar); _actualClosedDuration = MAX(0.000001f, _closedDuration + CCRANDOM_MINUS1_1() * _closedVar); duration = _actualOpenDuration + _actualClosedDuration; } -(void) update:(ccTime)t { float slice = _actualClosedDuration / duration; if (t < slice) [self showClosed]; else [self showOpen]; } -(void) showClosed { if (_closedFrame) { [target setVisible:YES]; [(CCSprite*)target setDisplayFrame:_closedFrame]; } else { // just hide the target [target setVisible:NO]; } } -(void) showOpen { if (_openFrame) { [target setVisible:YES]; [(CCSprite*)target setDisplayFrame:_openFrame]; } else { // just hide the target [target setVisible:NO]; } } -(CCIntervalAction*) reverse { // return 'self' return [EyeBlink actionWithOpenDuration:_openDuration openVar:_openVar closedDuration:_closedDuration closedVar:_closedVar openFrame:_openFrame closedFrame:_closedFrame repeatForever:_repeatForever]; } -(void) step:(ccTime) dt { [super step:dt]; if(_repeatForever && [super isDone]) { [self startWithTarget:target]; } } -(BOOL) isDone { return _repeatForever ? NO : [super isDone]; } @end