0

AtlasSpriteManagerとAltasSpriteを使用して、1つのファイルでフレームアニメーションごとにフレームを作成しています。最初はアニメーションのない単純な画像を表示するものを書きたいと思います。たとえば、それに触れると、アニメーションが表示され、最初の位置とフレームに戻ります。このコードを使用して、アニメーションなしで最初のフレームを表示することはできません。

Sprite *checker = [Sprite spriteWithFile:@"test.png"];
float frameWidth = [checker boundingBox].size.width / frameNumber;
float frameHeight = [checker boundingBox].size.height;

AtlasSpriteManager *mgr = [AtlasSpriteManager spriteManagerWithFile:fileName];
AtlasSprite *sprite = [AtlasSprite spriteWithRect:CGRectMake(pos.x, pos.y, frameWidth, frameHeight) spriteManager:mgr];
sprite.position = ccp(pos.x, pos.y);
[mgr addChild:sprite];

[layer addChild:mgr];

AtlasAnimation* animation = [AtlasAnimation animationWithName:@"Animation" delay:delay];
assert( animation != nil );

for(int i = 0; i < frameNumber; i++){
    [animation addFrameWithRect:CGRectMake(i * frameWidth, 0, frameWidth, frameHeight)];        
}

id action = [Animate actionWithAnimation:animation];
assert( action != nil );

id repeatAction;
if(repeatNumber == 0){
    repeatAction = [RepeatForever actionWithAction:action];
}else{
    repeatAction = [Repeat actionWithAction:action times:repeatNumber];
}

[sprite runAction:repeatAction];

それを行う方法はありますか?よろしくお願いします

4

2 に答える 2

0

更新された回答

あなたの質問が意図したとおりに理解できなかったと思います。

私が今理解しているように、あなたはあなたがアニメーションを持っているスプライトを持っています。ただし、アニメーションが終了しても、静止時にスプライトを設定するフレームには戻りません。

使用するコードactionWithAnimation:で、これをに設定してからactionWithAnimation:restoreOrigionalFrame:restoreOrigionalFrameパーツをYESに設定してみましたか?

これでアニメーションがレンダリングされますが、停止したら、アニメーションの前のフレームに戻ります。

または、アクションを実行し、アクションが停止しsetTextureRect:たら、AtlasSpriteを呼び出して手動で特定のフレームに戻ることもできます。

[sprite setTextureRect:CGRectMake( x, y, width, height )];

このマーカーの下に私の古い答えがあります:


あなたが今持っているコードはそれをすぐにアニメーション化します。

アニメーションをタッチで開始する場合は、タッチを確認する必要があります。次に、タッチを受け取るメソッドでアニメーションを開始するコードを追加します。

cocos2dのダウンロードにタッチの使用方法に関するサンプルコードがあります。

基本的に:スプライトをTargetedTouchDelegate作成し(またはそれを行うための新しいオブジェクトを作成しますが、それは少し面倒です)、実装します-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event

于 2009-11-21T02:12:59.620 に答える
0

シーケンスアクションと組み合わせたCallFuncアクションが必要だと思います。

あなたのコードから:


}
    .....
    .....
    id action = [Animate actionWithAnimation:animation];
    assert( action != nil );

    id repeatAction;
    if(repeatNumber == 0){
            repeatAction = [RepeatForever actionWithAction:action];
    }else{
            repeatAction = [Repeat actionWithAction:action times:repeatNumber];
    }
    id actionCallFunc = [CallFunc actionWithTarget:self selector:@selector(resetSprite)];
    id actionSequence = [Sequence actions: repeatAction, actionCallFunc, nil];

    [sprite runAction:repeatAction];
    ....
}

-(void)resetSprite{
    [sprite runAction:repeatAction];
}

最初にrepeatActionが実行され、それが終了すると、resetSpriteメソッドを呼び出してactionCallFuncが実行され、スプライトで必要なことを実行できます。

于 2009-11-30T12:36:34.640 に答える