gpt4 book ai didi

ios - 如何调用 SKLabelNode

转载 作者:行者123 更新时间:2023-11-28 07:24:08 24 4
gpt4 key购买 nike

我正在使用 SpriteKit 制作游戏。

我正在尝试添加一个 SKLabelNode (pointsLabel),如果我不设置 label.isHidden = true,它就可以正常工作。但是,我希望在 touchesBegan 之前隐藏 Label,但似乎我无法记忆起此函数中的 LabelNode,因此标签保持隐藏状态。

我如何让它工作?

这是代码:

class GameScene: SKScene {

private var pointsLabel: SKLabelNode?

override func didMove(to view: SKView) {
pointsLabelShow()
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// Get label node from scene and store it for use later
self.label = self.childNode(withName: "//pointsLabel") as? SKLabelNode
if let label = self.label {
label.isHidden = false
let fade = SKAction.fadeIn(withDuration: 0.2)
let sequence = SKAction.sequence([fade])
label.run(sequence)

}
}

override func update(_ currentTime: CFTimeInterval) {
/* Called before each frame is rendered */

self.label = self.childNode(withName: "//pointsLabel") as? SKLabelNode
if let label = self.label {
label.position.y = ballHeight + 840
}

func pointsLabelShow() {
let pointsLabel = SKLabelNode(fontNamed: "orkneymedium")
pointsLabel.isHidden = true
pointsLabel.text = "000"
pointsLabel.fontSize = 50
pointsLabel.fontColor = SKColor.white
self.addChild(pointsLabel)
}
}

最佳答案

尝试为 SKLabelNode 命名,假设为“pointsLabel”

func pointsLabelShow() {
let pointsLabel = SKLabelNode(fontNamed: "orkneymedium")
pointsLabel.isHidden = true
pointsLabel.text = "000"
pointsLabel.fontSize = 50
pointsLabel.name = "pointsLabel"
pointsLabel.fontColor = SKColor.white
self.addChild(pointsLabel)

}

如果您只希望节点在触摸时出现,请在您的 TouchsBegan 方法中执行以下操作:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first!
let positionInScene = touch.location(in: self)
let touchedNode = self.atPoint(positionInScene)

if let name = touchedNode.name
{
if name == "pointsLabel"
{
touchedNode.isHidden = false
let fade = SKAction.fadeIn(withDuration: 0.2)
let sequence = SKAction.sequence([fade])
touchedNode.run(sequence)
}
}
}

如果你想让节点在任何地方都能接触到,试着把节点放在一个局部变量中

希望对你有帮助

关于ios - 如何调用 SKLabelNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57080335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com