Spritekit - 重叠文本

时间:2014-06-02 05:24:13

标签: ios xcode sprite-kit

每次更新文本时,如何停止翻阅文本。每次文本更新时,它都会将文本放在上一个文本的顶部。这是我用来实现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;
    }
}

1 个答案:

答案 0 :(得分:1)

您的代码存在一些问题:

  • 您无需从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]]]];
}

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

相关问题