Calling method with animation from update function in sprite kit

天涯浪子 提交于 2019-12-13 19:57:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!