Search code examples
ioscocos2d-iphoneccsprite

code crashes when sprite go to the position


I use following code to show cloud randomly

- (void) cloudFly
{
  int whichcloud = (arc4random() % 2) + 1;
  CCSprite *target = [CCSprite spriteWithImageNamed:[NSString stringWithFormat:@"cloud%d.png",whichcloud]];
  target.opacity = 0.3;

int minY = target.contentSize.height / 2;
int maxY = winSize.height - target.contentSize.height / 2;
int rangeY = maxY - minY;
int actualY = (arc4random() % rangeY) + minY;

target.position = ccp(winSize.width + (target.contentSize.width/2), actualY);
[self addChild:target];

int minDuration = 1.0;
int maxDuration = 3.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;

// Create the actions
id actionMove = [CCActionMoveTo actionWithDuration:actualDuration position:ccp(-target.contentSize.width / 2, actualY)];
id actionMoveDone = [CCActionCallFunc actionWithTarget:self selector:@selector(cloudFinished:)];
[target runAction:[CCActionSequence actions:actionMove, actionMoveDone, nil]];
[_clouds addObject:target];
}

- (void)cloudFinished:(id)sender
{
  CCSprite *sprite = (CCSprite *)sender;
  [sprite stopAllActions];
  [self removeChild:sprite cleanup:YES];
}

but when the cloud flys to the position, this code will crash at line

- (void)cloudFinished:(id)sender

I use cocos2d-iphone v3.x, I try to comment/remove all codes in

- (void)cloudFinished:(id)sender

but it still crash. BWT, does ccsprite remove tag?


Solution

  • CCActionCallFunc is expecting a selector with no arguments, you should use CCActionCallBlock instead. There is an example in this answer.