【问题标题】:how to add an animation effect of drawing a circle in cocos2dcocos2d中如何添加画圆的动画效果
【发布时间】:2011-05-28 16:56:27
【问题描述】:

我正在开发一款游戏以找出两张图片之间的差异。

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

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

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

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

【问题讨论】:

  • 我还需要实现'错叉'的绘制效果。一行一行。

标签: iphone ios animation cocos2d-iphone


【解决方案1】:

作为一种简单的方法,我可以建议您创建一个圆圈并将其比例设为零。然后创建一个 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;
    } 
} 

【讨论】:

  • 嗯,不完全是我想要的。但如果我不知道如何制作绘图效果,绝对是一种简单的候选方法。 (就像实际绘图一样,从一个点开始画一个圆)
  • 您的意思是使用 for 循环,例如: [parts addObject: [CCSprite spriteWithFile: [NSString stringWithFormat: @"part%i.png", i]]];这意味着我需要创建许多 png 图像?
  • 这不是和用很多精灵图片创建动画一样吗?
  • @Let:不。我的意思是在 init 方法中创建它们并让它们全部透明。将它们放在正确的位置(因此制作圆圈)并添加到数组中以访问它们。然后在 update 方法中根据 localTime_ 更新它们的 alpha 值(透明度)。
  • 好的,我会试试看。但是创建 partN.png 文件会很麻烦。也许更好的方法是 opengl 的东西,比如水果忍者中的刀片效果。我在opengl上很烂...或者在我画画的时候记录电脑屏幕,我不认为我可以将mp4文件嵌入到游戏中。
【解决方案2】:

创建您想要的效果相当简单。 可以通过设置 CAShapeLayer 的 strokeEnd 来实现。

以下是详细信息:

  1. 创建一个路径,然后分配给 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];
    
  2. 设置 strokeEnd 以隐式动画绘制圆

    circle.strokeEnd = 1.0;

就是这样!圆圈将自动为您绘制;)。这是上面带有 GestureRecognizer 来触发动画的代码。将其复制到“Single View Application”类型的空项目并将其粘贴到 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;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多