Iphone 如何添加在cocos2d中绘制圆的动画效果

Iphone 如何添加在cocos2d中绘制圆的动画效果,iphone,ios,animation,cocos2d-iphone,Iphone,Ios,Animation,Cocos2d Iphone,我正在制作一个游戏来找出两张图片之间的差异 我想在你发现它时添加一个效果。画一个圆要比突然画圆好得多。但我以前从未做过核心动画或opengl 我不认为准备100个精灵并一帧一帧地改变精灵是个好主意。 这是我的代码:(只需在左右图像中添加一个圆形图像。) 那么我如何实现它呢?如果你能提供一些源代码,那就太好了 作为一种简单的方法,我建议您创建一个圆,并将其比例设置为零。然后创建一个CCScale操作并运行它。它会给你一个成长的循环。代码如下: CCSprite *sprite = [CCSprit

我正在制作一个游戏来找出两张图片之间的差异

我想在你发现它时添加一个效果。画一个圆要比突然画圆好得多。但我以前从未做过核心动画或opengl

我不认为准备100个精灵并一帧一帧地改变精灵是个好主意。 这是我的代码:(只需在左右图像中添加一个圆形图像。)


那么我如何实现它呢?如果你能提供一些源代码,那就太好了

作为一种简单的方法,我建议您创建一个圆,并将其比例设置为零。然后创建一个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;
    } 
} 
-(void)init
{
NSMUTABLEARRY*部件=[[NSMUTABLEARRY]retain];//将其存储在类变量中
零件=零件;
localTime\u0;//float localTime\u0-将本地时间存储在层中
//在此处创建所有零件,使其不可见并添加到图层和零件
}
-(void)update:(CCTime)dt//将更新方法添加到层中,该层将在每个模拟步骤中调用
{
localTime_uz+=dt;
常量浮点衰减时间=0.1;
int currentPart=localTime\uuu/fadeTime;
int i=0;
用于(CCSprite*零件中的零件)
{
//根据本地时间设置每个零件的不透明度
如果(i
创建您想要的效果相当简单。 您可以通过设置CAShapeLayer的strokeEnd来实现

详情如下:

  • 创建一条路径,然后将其指定给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

  • 就这样!圆圈将自动为您绘制;)。下面是上面带有手势识别器的代码,用于触发动画。将其复制到类型为“单视图应用程序”的空项目,并将其粘贴到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;
        }
    

    嗯,不完全是我想要的。但如果我不知道如何使绘图效果的话,这绝对是一个简单的候选方法。(就像实际的绘图一样,从一个点开始画一个圆)您的意思是使用for循环,例如:[parts addObject:[CCSprite spriteWithFile:[NSString stringWithFormat:@“part%i.png”,i]];这意味着我需要创建许多png图像?这与创建包含许多精灵图像的动画不一样吗?@Let:不。我的意思是在init方法中创建所有图像,并使它们都透明。将它们放在正确的位置(这样就可以创建圆)并添加到数组中以访问它们。然后在更新方法中,根据本地时间更新它们的alpha值(透明度)。好的,我将尝试tmr。但是创建partN.png文件会很麻烦。也许更好的方法是opengl的东西,比如《水果忍者》中的刀锋效果。我在opengl很烂…或者在我画画的时候记录下电脑屏幕,我不认为我可以在游戏中嵌入mp4文件。而且我还需要实现“错十字”的画图效果。一行一行。
        #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;
        }