标签在 Swift 中消失

Labels Disappearing in Swift

我在 spriteKit 的场景中有 2 个标签和 2 个滑块

当我触摸滑块时,它上面的标签消失了,我不知道为什么,

import SpriteKit
var bg = SKSpriteNode()

class Settings: SKScene {

let button   = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
let statButton   = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
let speedLabel = SKLabelNode()
let impulseLabel = SKLabelNode()
var sliderDemo = UISlider()
var gravityDemo = UISlider()
var speedLabelValue = SKLabelNode()
var impulseLabelValue = SKLabelNode()
override func didMoveToView(view: SKView) {
  backgroundImage()


    let height = CGFloat(view.bounds.height/2)
    let width = CGFloat(view.bounds.width/2)



// Change Bird Speed
    speedLabel.fontSize = 30
    speedLabel.text = "Change Speed:"
    speedLabel.position = CGPoint(x: width, y: height*2-60)
    addChild(speedLabel)

    sliderDemo.frame = CGRectMake(width-125, 33, 250, 100)
    sliderDemo.minimumValue = 1
    sliderDemo.maximumValue = 7
    sliderDemo.continuous = true
    sliderDemo.tintColor = UIColor.whiteColor()
    sliderDemo.value = 3.0
    sliderDemo.addTarget(self, action: "speedSlider:", forControlEvents: .ValueChanged)
    view.addSubview(sliderDemo)



// Change Bird Impulse (Gravity)
    impulseLabel.fontSize = 30
    impulseLabel.text = "Change Gravity:"
    impulseLabel.position = CGPoint(x: width, y: height*2-150)
    addChild(impulseLabel)

    gravityDemo.frame = CGRectMake(width-125, 120, 250, 100)
    gravityDemo.minimumValue = 60
    gravityDemo.maximumValue = gravMax
    gravityDemo.continuous = true
    gravityDemo.tintColor = UIColor.whiteColor()
    gravityDemo.value = gravMid
    gravityDemo.addTarget(self, action: "gravitySlider:", forControlEvents: .ValueChanged)
    view.addSubview(gravityDemo)








    let image2 = UIImage(named: "arrowright") as UIImage!
    button.frame = CGRectMake(width*1.5-37.5, height*1.51, 75, 75)
    button.setTitle("Play Game", forState: UIControlState.Normal)
    button.setImage(image2, forState: .Normal)
    button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
    button.addTarget(self, action: "GameSceneFunc", forControlEvents: UIControlEvents.TouchUpInside)
    button.alpha = 1
    view.addSubview(self.button)



    // Stat Button
    let image = UIImage(named: "stats") as UIImage!
    statButton.frame = CGRectMake(width/2-37.5, height*1.5, 75, 75)
    statButton.setTitle("View Statistics...", forState: UIControlState.Normal)
    statButton.setImage(image, forState: .Normal)
    statButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
    statButton.addTarget(self, action: "Statistics", forControlEvents: UIControlEvents.TouchUpInside)
    statButton.alpha = 1
    view.addSubview(self.statButton)



}









func speedSlider(sender:UISlider!)
{
    var sValue = Int32(sender.value)
    speedLabelValue.text = String(sValue)


    if (sValue == 1) {
        sValue = 7
    } else if (sValue == 2) {
        sValue = 6
    } else if (sValue == 3) {
        sValue = 5
    } else if (sValue == 4) {
        sValue = 4
    } else if (sValue == 5) {
        sValue = 3
    } else if (sValue == 6) {
        sValue = 2
    } else if (sValue == 7) {
        sValue = 1
    }

    dur1 = Double(sValue)+0.5
    dur2 = Double(sValue)+0.5


}







func gravitySlider(sender:UISlider!) {
    var sValue2 = Int32(sender.value)
    impulseLabelValue.text = String(sValue2)


    impulse = CGFloat(sValue2)


}

澄清一下 - 当我进入场景后点击滑块时,它上面的标签会消失,当我触摸另一个滑块时,标签不会消失,无论我先触摸哪个,链接到它的标签都是改变的那个

在您的代码片段中,我没有看到您添加 speedLabelValue:

speedLabelValue.fontSize = 30
speedLabelValue.fontColor = UIColor.whiteColor()
speedLabelValue.text = "v"
speedLabelValue.position = CGPoint(x: width, y: height*2-120)
addChild(speedLabelValue)

我也没有看到您添加 impulseLabelValue

impulseLabelValue.fontSize = 30
impulseLabelValue.text = "g"
impulseLabelValue.position = CGPoint(x: width, y: height*2-210)
addChild(impulseLabelValue)

但是,就我而言,当我添加这两个标签时,它更新得很好。

我还通过一次手动调用 .ValueChanged 方法来初始化这两个标签:

speedSlider(sliderDemo)
gravitySlider(gravityDemo)