几次运行后,Cocos2d CCAction停止

时间:2011-12-29 07:47:22

标签: iphone xcode cocos2d-iphone

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

        } 
    }
}

我安排了一个名为imaged blink的方法,每5秒钟调用一次。该方法将使我的sprite从数组闪烁(出现和消失)。但经过几次调用(大约10次或更多)后,精灵停止闪烁。但是,通过NSLog“闪烁”的输出仍然以5s间隔出现。我在iPhone模拟器和我的iPod上都有这个问题。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

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

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

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