2011-12-19 38 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];