如何在按钮上显示文字? -SpriteKit

时间:2017-02-11 22:20:46

标签: swift sprite-kit

我是SpriteKit的新手,我正在创建一个游戏,我在其中设计了带有草图的主菜单,并将图像应用于spritekit作为我的背景图像。每次我在按钮图像上方放置标签/文字。它没有正常工作。当他们点击播放时可以说,他们会玩,当他们输了,并突然重新开始标签消失。

这是一张图片:

Image

这是我的代码

    //creating the start game programmatically. 
    let dw_startButton = SKSpriteNode()

    dw_startButton.name = "dw_startbutton"

    dw_startButton.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
    self.addChild(dw_startButton)

    //Instructions Button
    let dw_selector = SKSpriteNode()
    dw_selector.name = "dw_selector"

    dw_selector.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
    self.addChild(dw_selector)

    //Starting text
    let startText = SKLabelNode(text: "Play!")
    startText.fontColor = UIColor.white
    startText.position = CGPoint(x: 50, y: 50)
    startText.fontSize = 45
    startText.fontName = "Helvetica-Bold"
    startText.verticalAlignmentMode = SKLabelVerticalAlignmentMode(rawValue: 1)!
    startText.name = "dw_startbutton"
    dw_startButton.addChild(startText)

    //instructions text
    let startTexts = SKLabelNode(text: "Insturctions")
    startTexts.fontColor = UIColor.white
    startTexts.position = CGPoint(x: 0, y: 0)
    startTexts.fontSize = 20
    startTexts.fontName = "Helvetica-Bold"
    startTexts.verticalAlignmentMode = SKLabelVerticalAlignmentMode(rawValue: 1)!
    startTexts.name = "dw_selector"
    dw_selector.addChild(startTexts)

1 个答案:

答案 0 :(得分:0)

您始终需要将zPosition添加到精灵和标签中。否则有时它们会出现,有时它们可​​能会出现在背景之后

//creating the start game programmatically. 
let dw_startButton = SKSpriteNode()
dw_startButton.zPosition = 1
dw_startButton.name = "dw_startbutton"
dw_startButton.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
self.addChild(dw_startButton)

//Instructions Button
let dw_selector = SKSpriteNode()
dw_selector.name = "dw_selector"
dw_selector.zPosition = 1
dw_selector.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
self.addChild(dw_selector)

//Starting text
let startText = SKLabelNode(text: "Play!")
startText.fontColor = UIColor.white
startText.position = CGPoint(x: 50, y: 50)
startText.fontSize = 45
startText.fontName = "Helvetica-Bold"
startText.verticalAlignmentMode = SKLabelVerticalAlignmentMode(rawValue: 1)!
startText.name = "dw_startbutton"
startText.zPosition = 2
dw_startButton.addChild(startText)

//instructions text
let startTexts = SKLabelNode(text: "Insturctions")
startTexts.fontColor = UIColor.white
startTexts.position = CGPoint(x: 0, y: 0)
startTexts.fontSize = 20
startTexts.fontName = "Helvetica-Bold"
startTexts.verticalAlignmentMode = SKLabelVerticalAlignmentMode(rawValue: 1)!
startTexts.name = "dw_selector"
startTexts.zPosition = 2
dw_selector.addChild(startTexts)

//new button
let newButton = SKSpriteNode(imageNamed: "button_back")
newButton.zPosition = 1
newButton.name = "button1"
newButton.position = CGPoint(x: 100, y: 100)
self.addChild(newButton)

let buttonText = SKLabelNode(text: "Play")
buttonText.fontColor = .white
buttonText.fontSize = 20
buttonText.fontName = "Helvetica-Bold"
buttonText.verticalAlignmentMode = .center
buttonText.horizontalAlignmentMode = .center
buttonText.name = "dw_selector"
buttonText.zPosition = 2
newButton.addChild(buttonText)