问题
so I have this method:
-(void)levelLabel {
SKLabelNode *levelOne = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
levelOne.fontSize = 25;
levelOne.fontColor = [SKColor whiteColor];
levelOne.text = @"Level 1";
levelOne.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
[self addChild:levelOne];
SKAction *stageNumberIn = [SKAction fadeInWithDuration:1.0];
SKAction *stageNumberOut = [SKAction fadeOutWithDuration:1.5];
SKAction *remove = [SKAction removeFromParent];
SKAction *sequence = [SKAction sequence:@[stageNumberIn, stageNumberOut, remove]];
[levelOne runAction:sequence];
}
and I try to call it in the "update" function like this:
-(void)update:(CFTimeInterval)currentTime {
[self levelLabel];
}
but it won't work. The label works without the animation of fading. If I put the SKLabelNode in the initWithSize:
-(id)initWithSize:(CGSize)size {
...
}
it works perfect.
Can someone tell me where I went wrong?
回答1:
Your levelLabel
method is called every frame update which will cause the action to be executed every frame.
You need to run it only once. So either remove it from the update or add the following check before running the action :
if (![levelOne hasActions]) {
[levelOne runAction:sequence];
}
The preferred way will be to remove it from the update method and perform it only when necessary as you are creating the label on each frame (and add it every time to the scene which will bloat your nodes count as well) when you can create it only once and update it only when necessary
来源:https://stackoverflow.com/questions/22655149/calling-method-with-animation-from-update-function-in-sprite-kit