【问题标题】:Animate CCSprite动画 CCSprite
【发布时间】:2012-05-15 10:09:10
【问题描述】:

我是 cocos2d 的新手,正在准备我的演示游戏。我只使用一个图像从右到左移动一个精灵,比如从左到右移动的鸟图像。但我想通过各种图像为该精灵设置动画,使其看起来像一只飞翔的鸟。我不知道如何做到这一点。

这是我的代码:

    CCSprite *target = [CCSprite spriteWithFile:@"Target.png" rect:CGRectMake(0, 0, 27, 40)]
id actionMove = [CCMoveTo actionWithDuration:actualDuration position:ccp(-target.contentSize.width/2, actualY)];
    id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)];
    [target runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];

【问题讨论】:

    标签: cocos2d-iphone


    【解决方案1】:

    用于动画特定精灵你需要精灵表在你的资源中。您可以使用我通常使用的Texture PackerZwoptex 工具创建 Sprite-sheet。

    接下来你可以实现下面的代码

        CCSpriteBatchNode *sheet = [CCSpriteBatchNode batchNodeWithFile:@"drawing1-i3.png"]; // Png File Name
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"drawing1-i3.plist"]; // Plist File Name
        [self addChild:sheet];
    
            //Generating the Animation 
        NSMutableArray *arr_anim =[NSMutableArray array];
        for(int i=1; i<30; i++) // i< number of frames in the plist File Name
        {
            NSString *str_fileNm = [NSString stringWithFormat:@"drawing1%d.png",i];
            CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:str_fileNm];
            [arr_anim addObject:frame];
        }
    
        CCSprite *startAnim = [CCSprite spriteWithSpriteFrameName:@"drawing11.png"];
        [startAnim setPosition:ccp(150,150)];
        [self addChild:startAnim];
    
        //Starting the Animation 
        CCAnimation *animation = [CCAnimation animationWithFrames:arr_anim delay:0.15f];
        // id action =[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation restoreOriginalFrame:YES]];
        id action =[CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO];   
        [startAnim runAction:action];
    

    我认为它会帮助您创建动画。

    【讨论】:

    • 精灵表不是动画的要求
    • startAnim 精灵应该作为批处理节点的子节点添加,这样它只使用单个 OpenGL 调用或“批处理绘制”。
    【解决方案2】:

    使用CCAnimation class.

    特别是,使用animationWithFrames: 之类的方法并将图像作为数组提供。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多