2011-12-19 10 views
0

基本的に、私はスプライトをアニメーション化しようとしています。右に動くときは、あるアニメーションを再生し、別のアニメーションを再生するようにします。ここに私のコードがあります - 何も起こっていない、スプライトは単に画面に表示されます。ココス2dアクション - CCallFunc何もしない

-(void)moveLeft{ 

[sprite stopActionByTag:0]; 

NSMutableArray *spriteFrames2 = [NSMutableArray array]; 

CCSpriteFrame *frame4 = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"KnightSpritesLeft10.png"]; 
CCSpriteFrame *frame5 = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"KnightSpritesLeft11.png"]; 
CCSpriteFrame *frame6 = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"KnightSpritesLeft12.png"]; 


[spriteFrames2 addObjectsFromArray:[NSArray arrayWithObjects:frame4,frame5,frame4,frame6, nil]]; 

CCAnimation *anim = [CCAnimation animationWithFrames:spriteFrames2 delay:0.15f]; 
CCAnimate *animate = [CCAnimate actionWithAnimation:anim]; 
CCRepeatForever *repeat = [CCRepeatForever actionWithAction:animate]; 
repeat.tag = 1; 
[sprite runAction:repeat]; 

id moveToLeft = [CCMoveTo actionWithDuration:3.0 position:CGPointMake(0, sprite.position.y)]; 
[sprite runAction:moveToLeft]; 



} 
-(void)moveRight{ 

[sprite stopActionByTag:1]; 

CGSize winSize = [[CCDirector sharedDirector]winSize]; 


NSMutableArray *spriteFrames = [NSMutableArray array]; 
CCSpriteFrame *frame1 = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"KnightSpritesRight6.png"]; 
CCSpriteFrame *frame2 = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"KnightSpritesRight4.png"]; 
CCSpriteFrame *frame3 = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"KnightSpritesRight5.png"]; 

[spriteFrames addObjectsFromArray:[NSArray arrayWithObjects:frame1,frame2,frame1,frame3, nil]]; 

CCAnimation* anim = [CCAnimation animationWithFrames:spriteFrames delay:0.15f]; 
CCAnimate* animate = [CCAnimate actionWithAnimation:anim]; 
CCRepeatForever *repeat = [CCRepeatForever actionWithAction:animate]; 
repeat.tag = 0; 
[sprite runAction:repeat]; 

id moveToRight = [CCMoveTo actionWithDuration:3.0 position:CGPointMake(winSize.width, sprite.position.y)]; 
[sprite runAction:moveToRight]; 


} 

-(id) init 
{ 



if((self=[super init])) { 


    [[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:@"KnightImages2.plist"]; 

    sprite = [CCSprite spriteWithSpriteFrameName:@"KnightSpritesRight6.png"]; 
    sprite.position = ccp(240, 180); 

    [self addChild:sprite]; 

    id actionSequence = [CCSequence actions:[CCCallFunc actionWithTarget:self selector:@selector(moveRight)],[CCCallFunc actionWithTarget:self selector:@selector(moveLeft)], nil]; 
    CCRepeatForever *repeatForever = [CCRepeatForever actionWithAction:actionSequence]; 

    [sprite runAction:repeatForever]; 
} 
return self; 
} 

答えて

1

これを設定した方法は、以下の問題が発生したことをフレームごとである:1

  • フレームがmoveRight

  • がmoveLeft
を実行実行

フレーム2

  • moveLeft

無限に実行moveRight

  • を実行します。

    moveRightまたはmoveLeftを呼び出すたびにアニメーションを開始するので、実際には何も起こりません。実際に再生するために別のアニメーションを実行する前にしばらく待つ必要があります。これにはCCDelayTimeアクションを使用できます。

    id actionSequence = [CCSequence actions: 
        [CCCallFunc actionWithTarget:self selector:@selector(moveRight)], 
        [CCDelayTime actionWithDuration:2], 
        [CCCallFunc actionWithTarget:self selector:@selector(moveLeft)], 
        [CCDelayTime actionWithDuration:2], 
        nil]; 
    
  • 関連する問題