Sunteți pe pagina 1din 2

how to add an animation effect of drawing a circle in cocos2d -(void) show { CCSprite* leftCircle = [CCSprite spriteWithFile:@"circle.

png"]; CCSprite* rightCircle = [CCSprite spriteWithFile:@"circle.png"]; leftCircle.scaleX = size.width / [leftCircle boundingBox].size.width; leftCircle.scaleY = size.height / [leftCircle boundingBox].size.height; rightCircle.scaleX = size.width / [rightCircle boundingBox].size.width; rightCircle.scaleY = size.height / [rightCircle boundingBox].size.height; leftCircle.anchorPoint = ccp(0, 1); rightCircle.anchorPoint = ccp(0, 1); leftCircle.position = leftPosition; rightCircle.position = rightPosition; [[GameScene sharedScene] addChild:leftCircle z: 3]; [[GameScene sharedScene] addChild:rightCircle z: 3]; shown = YES; } CCSprite *sprite = [CCSprite spriteWithFile:@"mySprite.png"]; [sprite setScale:0.01]; id scale = [CCScale actionWithDuration:0.3 scale:1]; [sprite runAction:scale]; CCSprite *sprite = [CCSprite spriteWithFile:@"mySprite.png"]; [sprite setOpacity:0]; id fade = [CCFadeIn actionWithDuration:0.3]; [sprite runAction:fade]; [sprite runAction:fade]; [sprite runAction:scale]; -(void) init { NSMutableArray *parts = [[NSMutableArray array] retain]; //store it in your class variable parts_ = parts; localTime_ = 0; //float localTime_ - store the local time in your layer //create all the parts here, make them invisible and add to the layer and pa rts } -(void) update: (CCTime) dt //add update method to your layer that will be calle d every simulation step { localTime_ += dt; const float fadeTime = 0.1; int currentPart = localTime_ / fadeTime; int i = 0; for (CCSprite *part in parts) { //setup the opacity of each part according to localTime if (i < currentPart) [part setOpacity:255]; else if (i == currentPart) { float localLocalTime = localTime - i*fadeTime; float alpha = localLocalTime / fadeTime; [part setOpacity:alpha]; } ++i;

} } UIBezierPath* circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100 .0, 100.0) radius:80.0 startAngle:DEGREES_TO_RADIANS(270) endAngle:DEGREES_TO_RA DIANS(270.01) clockwise:NO]; circle = [CAShapeLayer layer]; circle.path = circlePath.CGPath; circle.strokeColor = [[UIColor blackColor] CGColor]; circle.fillColor = [[UIColor whiteColor] CGColor]; circle.lineWidth = 6.0; circle.strokeEnd = 0.0; [self.view.layer addSublayer:circle]; #define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI) - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIBezierPath* circlePath = [UIBezierPath bezierPathWithArcCenter:CGPoint Make(100.0, 100.0) radius:80.0 startAngle:DEGREES_TO_RADIANS(270) endAngle:DEGRE ES_TO_RADIANS(270.01) clockwise:NO]; circle = [CAShapeLayer layer]; circle.path = circlePath.CGPath; circle.strokeColor = [[UIColor blackColor] CGColor]; circle.fillColor = [[UIColor whiteColor] CGColor]; circle.lineWidth = 6.0; circle.strokeEnd = 0.0; [self.view.layer addSublayer:circle]; // add a tag gesture recognizer // add a single tap gesture recognizer UITapGestureRecognizer* tapGR = [[UITapGestureRecognizer alloc] initWith Target:self action:@selector(handleTapGesture:)]; [self.view addGestureRecognizer:tapGR]; } #pragma mark - gesture recognizer methods // // GestureRecognizer to handle the tap on the view // - (void)handleTapGesture:(UIGestureRecognizer *)gestureRecognizer { circle.strokeEnd = 1.0; }

S-ar putea să vă placă și