Cocos2d CCAction运行几次后停止


-(id) init
{
    // always call "super" init
    // Apple recommends to re-assign "self" with the "super" return value
    if( (self=[super initWithColor:ccc4(255,255,255,255)] )) {
        [UIApplication sharedApplication].idleTimerDisabled = YES;
        imageArray = [[NSMutableArray alloc]init];
        winSize = [[CCDirector sharedDirector]winSize];
        [self addEverything];
        [self schedule:@selector(imageBlink) interval:5.0f];
    }
    return self;
}
-(void)imageBlink{
    int tagNum = (arc4random() %9 )+1 ;
    for (CCSprite *object  in imageArray) {
        if (object.tag == tagNum) {
            [object runAction: [CCSequence actions:[CCBlink actionWithDuration:2 blinks:1],[CCFadeOut actionWithDuration:2], nil]];
            [[SimpleAudioEngine sharedEngine]playEffect:@"slap.mp3"];
            NSLog(@"blink");
            return;
        }
    }
}
-(void)addImage{
    self.imageTag = 1;
    for (int i = 0; i <3; i++) {
        for (int j =0; j<3; j++) {
            image = [CCSprite spriteWithSpriteFrameName:@"0002.png"];
            NSLog(@"%d",imageTag);
            [image setTag:self.imageTag];
            self.imageTag ++ ;
            image.position = ccp(STAGE_WIDTH/(3)*(j)+35+41, STAGE_HEIGHT/(3)*(i)+115+41);
            [imageArray addObject:image];
            [image setVisible:NO];
            [self addChild:image z:2];
        } 
    }
}

我安排了一个名为imageblink的方法每5秒调用一次。该方法将使我的精灵从数组中闪烁(出现和消失)。但在几次呼叫后(大约10次或更多),精灵停止闪烁。但是,NSLog的"blink"输出仍然每隔5s出现一次。我在iphone模拟器和ipod上都有这个问题。谢谢你的帮助。

如果一个对象已经在运行闪烁序列,而您在运行另一个对象,这可能会干扰屏幕上正在发生的事情。我建议在运行新序列之前添加stopAllActions。

我注意到的另一件事是你运行一个CCFadeOut动作。请记住,在这个动作完成后,精灵的不透明度将为0,CCBlink改变了可见属性,但不影响不透明度。这意味着你应该确保精灵在开始序列之前处于最大不透明度:

[object stopAllActions];
object.opacity = 255;
[object runAction: [CCSequence actions:[CCBlink actionWithDuration:2 blinks:1],[CCFadeOut actionWithDuration:2], nil]];

最新更新