0

私は 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]];
4

2 に答える 2

3

特定のスプライトをアニメーション化するには、リソースにスプライトシートが必要です。スプライト シートは、私が通常使用するTexture PackerまたはZwoptex Tools から作成できます。

次に、以下のコードを実装できます

    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];

アニメーション制作の参考になれば幸いです。

于 2012-05-15T12:59:02.593 に答える
1

CCAnimation クラスを使用します。

特に、 のようなメソッドを使用animationWithFrames:して、画像を配列として提供します。

于 2012-05-15T10:47:51.483 に答える