Cocos-2d动作 - CCallFunc没有做任何事情

时间:2011-12-19 21:12:29

标签: iphone ios cocos2d-iphone

基本上,我正在尝试为精灵制作动画。当向右移动时我想要播放一个动画,当向左移动另一个动画时播放。这是我的代码 - 什么都没发生,精灵只显示在屏幕上。

-(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 个答案:

答案 0 :(得分: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];