2011-12-19 3 views
0

В принципе, я пытаюсь анимировать спрайт. Когда я двигаюсь вправо, я хочу, чтобы одна анимация играла, а при движении оставила другую, чтобы играть. Вот мой код - ничего не происходит вообще, спрайт просто отображается на экране.Действия Cocos-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

  • выполнить moveRight
  • выполнить moveLeft

бесконечности объявлений.

С тех пор, как вы запускаете анимацию во все времена, когда вы вызываете 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]; 
Смежные вопросы