How to add an animated circle drawing effect in cocos2d

I am working on a game to determine the difference between two images.

I want to add an effect when you notice it. plotting a circle would be much better than just showing a circle all of a sudden. but I have never done basic animation or opengl before.

I don’t think that preparing 100 sprites and changing the frame of a sprite by frame is a good idea. here is my code: (just add the circle image to the left and right image.)

-(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;
}

So how can I implement this? It would be great if you could provide some source code.

+3
source share
2 answers

, . CCScale . . :

CCSprite *sprite = [CCSprite spriteWithFile:@"mySprite.png"];
[sprite setScale:0.01];
id scale = [CCScale actionWithDuration:0.3 scale:1];
[sprite runAction:scale];

CCFadeIn. :

CCSprite *sprite = [CCSprite spriteWithFile:@"mySprite.png"];
[sprite setOpacity:0];
id fade = [CCFadeIn actionWithDuration:0.3];
[sprite runAction:fade];

:

[sprite runAction:fade];
[sprite runAction:scale];

(, 3) . , .

, (), . , , , , . - :

-(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 parts
}

-(void) update: (CCTime) dt //add update method to your layer that will be called 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;
    } 
} 
+2

. , strokeEnd CAShapeLayer.

:

  • , CAShapeLayer

    UIBezierPath* circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100.0, 100.0) radius:80.0 startAngle:DEGREES_TO_RADIANS(270) endAngle:DEGREES_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];
    
  • strokeEnd

    circle.strokeEnd = 1.0;

! ;). GestureRecognizer . " " ViewController.

    #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:CGPointMake(100.0, 100.0) radius:80.0 startAngle:DEGREES_TO_RADIANS(270) endAngle:DEGREES_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] initWithTarget: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;
    }
+1

All Articles