我正在开发一个sprite kit游戏,我在其中有一个名为playCountdown的动作:
SKAction *playCountdown = [SKAction performSelector:@selector(startCountdown) onTarget:self];
playCountdown工作正常,但完成后,永远不会调用完成块。
[self runAction:playCountdown completion:^{
NSLog(@"anything goes");
}];
这是完整性的startCountdown:
- (void)startCountdown{
SKLabelNode *countdownLabel = [SKLabelNode labelNodeWithFontNamed:@"Arial"];
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
__block short countdownValue = 3;
countdownLabel.position = CGPointMake(screenWidth/2, screenHeight/2 - countdownLabel.fontSize/2);
countdownLabel.zPosition = 100.0;
countdownLabel.text = [NSString stringWithFormat:@"%d", countdownValue];
countdownLabel.fontColor = [UIColor blackColor];
countdownLabel.verticalAlignmentMode = SKLabelVerticalAlignmentModeBaseline;
[self addChild:countdownLabel];
SKAction *zoomIn = [SKAction scaleTo:1.5f duration:0.9];
SKAction *fadeOut = [SKAction fadeAlphaTo:0.0f duration:0.1];
SKAction *updateCount = [SKAction customActionWithDuration:0.0 actionBlock:^(SKNode *node, CGFloat elapsedTime) {
SKLabelNode *textNode = (SKLabelNode *)node;
countdownValue --;
if (countdownValue == 0) {
textNode.text = @"PLAY!";
}
else{
textNode.text = [NSString stringWithFormat:@"%d", countdownValue];
}
textNode.alpha = 1.0f;
textNode.xScale = 1.0f;
textNode.yScale = 1.0f;
}];
SKAction *sequence = [SKAction sequence:@[zoomIn, fadeOut, updateCount]];
SKAction *repeat = [SKAction repeatAction:sequence count:4];
[countdownLabel runAction:repeat completion:^{
[countdownLabel removeFromParent];
}];
NSLog(@"completed!");
}
我已经确认startCountdown实际上已到达终点。但是完成块永远不会被调用。有什么想法吗?
由于
答案 0 :(得分:0)
我建议您通过创建一个非常简单的仅NSLog块来验证您的代码是否确实被调用。
如果您仍然遇到问题,请考虑改用SKAction sequence。这将完成与您尝试使用完成块相同的事情。
答案 1 :(得分:0)
使用选择器创建的SKAction必定存在问题...代码:
SKAction *playCountdown = [SKAction performSelector:@selector(startCountdown) onTarget:self];
将执行调用:
[self runAction:playCountdown completion:^{ ... }];
虽然完成块永远不会被调用。
我改为使用块创建了playCountdown动作:
SKAction *playCountdown = [SKAction runBlock:^{
[self startCountdown];
}];
现在调用完成块。现在的问题是,playCountdown动作的结束并没有被理解为#34;系统和2个动作不是连续运行,而是几乎同时运行。