【发布时间】:2016-02-28 09:38:16
【问题描述】:
我个人认为这真的 真的很奇怪。
我只是在使用 SpriteKit 开发游戏。我创建了SKSpriteNode 的子类,以创建一个显示游戏剩余时间、分数等的精灵。我将其命名为GameBanner。
我现在正尝试在横幅的左上角添加SKLabelNode。我不知道它的确切位置,所以我写了这个:
timerTitle.position = //CGPointMake(CGRectGetMinX(self.frame) +
timerTitle.frame.width / 2 + 20, CGRectGetMaxY(self.frame) -
self.frame.height / 2 - 20)
// timerTitle is the label node that shows "Time Left"
但我只能在屏幕上看到横幅的背景图片,而不是“剩余时间”。我认为这是因为我以某种方式将timerTitle 的位置设置在横幅之外。
然后我想,好吧,这对我来说很难确定它应该在哪里,让我们“试错”吧!
我首先尝试使用 (0, 0)。它将标签放在中心。然后我尝试了这些:
- (-100, 0) - 标签不可见(“也许它离左边太远了?”我想)
- (-40, 0) - 标签在中心附近,向左偏移一点(“如果 -40 是 this 向左一点,那么 -100 就可以了!”,我想)
- (-100, 0) - 标签可见,似乎在正确的位置。 (“等一下!为什么第一次没有用?”,我想)
- (当我尝试使用 (-160, 0) 和 (-160, 20) 时发生同样的事情,即第一次不可见时,将其更改为另一个值并运行它,将其更改回 (- 160, 0) 并且有效)
希望你能理解我的截图问题:
这是我第一次将位置设置为 (-160, 20):
然后我把它改成(-160, 0),就出现了:
在那之后,我把它改回 (-160, 20),它神奇地起作用了!
这是GameBanner 类:
class GameBanner: SKSpriteNode {
let timerTitle = SKLabelNode(text: "Time Left:")
let timerLabel = SKLabelNode(text: "60")
let collectedBlocksLabel = SKLabelNode(text: "Collected Blocks")
let scoreLabel = SKLabelNode(text: "Score:")
override init(texture: SKTexture?, color: UIColor, size: CGSize) {
super.init(texture: texture, color: color, size: size)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//Call this initializer with Position: (302.08, 860.78), Size: (604.16, 100.0) to reproduce
convenience init(position: CGPoint, size: CGSize) {
self.init(texture: SKTexture(imageNamed: "banner"), color: UIColor.clearColor(), size: size)
self.position = position
configureLabels(timerTitle, timerLabel, collectedBlocksLabel, scoreLabel)
// This is the part that doesn't work as I have said above
timerTitle.position = CGPointMake(-160, 20)
addChild(timerTitle)
//addChild(timerLabel)
//addChild(collectedBlocksLabel)
// addChild(scoreLabel)
zPosition = 10000
}
private func configureLabels(labels: SKLabelNode...) {
for label in labels {
label.fontColor = UIColor.whiteColor()
label.fontName = "Chalkduster"
label.fontSize = 25
}
}
}
这是横幅的背景图片。也许这很重要:
【问题讨论】:
标签: ios swift sprite-kit position coordinates