Jordan Brown
Jordan Brown

Reputation: 638

Cocos-2d actions -- CCallFunc not doing anything

Basically, I'm trying to animate a sprite. When moving right I want one animation to play, and when moving left another to play. Here's my code-- nothing is happening at all, the sprite is merely shown on the screen.

-(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;
}

Upvotes: 0

Views: 422

Answers (1)

CodeSmile
CodeSmile

Reputation: 64477

The way you've set this up is that the following happens, frame by frame:

Frame 1

  • execute moveRight
  • execute moveLeft

Frame 2

  • execute moveRight
  • execute moveLeft

Ad infinitum.

Since you're starting the animation all over every time you call moveRight or moveLeft, nothing really happens. You need to wait some time before running another animation for it to actually play. You can use the CCDelayTime action for this:

id actionSequence = [CCSequence actions:
  [CCCallFunc actionWithTarget:self selector:@selector(moveRight)],
  [CCDelayTime actionWithDuration:2],
  [CCCallFunc actionWithTarget:self selector:@selector(moveLeft)], 
  [CCDelayTime actionWithDuration:2],
  nil];

Upvotes: 1

Related Questions