每次更新文本时如何停止重叠文本。每次文本更新时,它将把文本放在前一个文本的顶部。这是我用来实现HUD的代码。

HUD设定

-(void)createHUD {
    id wait = [SKAction waitForDuration:1.5];
    id run = [SKAction runBlock:^ {
    //delete after 1.5 seconds then reset it so that it doesnt overlap cuase its really annoying to watch that happen

    counter++;
    updateLabel = true;

    counterLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
    counterLabel.name = @"myCounterLabel";
    //counterLabel.text = @"0";
    counterLabel.fontSize = 50;
    counterLabel.fontColor = [SKColor yellowColor];
    counterLabel.position = CGPointMake(self.size.width / 2.0f, self.size.height / 1.3f);

    [self addChild: counterLabel];
}];
    [self runAction:[SKAction sequence:@[wait, run]]];
    [self runAction:[SKAction repeatActionForever:[SKAction sequence:@[wait, run]]]];
}


更新方式

    -(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */
    if(updateLabel == true) {
    counterLabel.text = [NSString stringWithFormat:@"%i",counter];
    updateLabel = false;
    }
}

最佳答案

您的代码存在一些问题:


您不需要通过update方法来更新counterLabel。这只是在更新循环中添加了额外的处理,以便只需要在1.5秒内执行一次即可。
您可以从块本身内部更新标签。这将使您的实现更加简单。


看一下下面的方法。它应该能够实现您的尝试。

-(void)createHUD {

    counterLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
    counterLabel.name = @"myCounterLabel";
    //counterLabel.text = @"0";
    counterLabel.fontSize = 50;
    counterLabel.fontColor = [SKColor yellowColor];
    counterLabel.position = CGPointMake(self.size.width / 2.0f, self.size.height / 1.3f);

    [self addChild: counterLabel];

    id wait = [SKAction waitForDuration:1.5];
    id run = [SKAction runBlock:^ {

        counter++;
        counterLabel.text = [NSString stringWithFormat:@"%i",counter];


    }];
    [self runAction:[SKAction sequence:@[wait, run]]];
    [self runAction:[SKAction repeatActionForever:[SKAction sequence:@[wait, run]]]];
}


注意:从更新方法中删除与计数器标签有关的所有代码。

10-08 12:13