在 Sprite Kit 中创建在屏幕上保持静态的 HUD
Create HUD in Sprite Kit that remains static on screen
我试图在本教程的跳跃游戏中创建一个静态 HUD,它在整个游戏过程中始终位于屏幕顶部。我对 Swift 还是个新手,所以也许这是我不容易看到的约定:
http://www.raywenderlich.com/87232/make-game-like-mega-jump-sprite-kit-swift-part-2
在教程中,他将HUD放在override init(size: CGSize)
函数中:
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(size: CGSize) {
super.init(size: size)
...
// Build the HUD
// HUD
hudNode = SKNode()
hudNode.zPosition = 1000
addChild(hudNode)
// Coins
// 1
let coin = SKSpriteNode(imageNamed: "powerup05_1")
coin.position = CGPoint(x: 300, y: self.size.height-100)
coin.zPosition = 1000
hudNode.addChild(coin)
// 2
lblCoins = SKLabelNode(fontNamed: "ChalkboardSE-Bold")
lblCoins.fontSize = 70
lblCoins.fontColor = SKColor.whiteColor()
lblCoins.position = CGPoint(x: 375, y: self.size.height-100)
lblCoins.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Left
lblCoins.zPosition = 1000
// 3
lblCoins.text = String(format: "X %d", GameState.sharedInstance.coins)
hudNode.addChild(lblCoins)
// Score
// 4
lblScore = SKLabelNode(fontNamed: "ChalkboardSE-Bold")
lblScore.fontSize = 70
lblScore.fontColor = SKColor.whiteColor()
lblScore.position = CGPoint(x: self.size.width-325, y: self.size.height-100)
lblScore.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Right
lblScore.zPosition = 1000
// 5
lblScore.text = "0"
hudNode.addChild(lblScore)
}
但是,如果我尝试在我的游戏中使用此功能,我会收到错误消息:
Argument labels '(fileNamed:)' do not match any available overloads
在这个函数中:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
switch gameState.currentState {
case is WaitingForTap:
gameState.enterState(WaitingForBomb)
// Switch to playing state
self.runAction(SKAction.waitForDuration(2.0),
completion:{
self.gameState.enterState(Playing)
})
case is GameOver:
// Error! Argument labels '(fileNamed:)' do not match any available overloads
let newScene = GameScene(fileNamed:"GameScene")
newScene!.scaleMode = .AspectFill
let reveal = SKTransition.flipHorizontalWithDuration(0.5)
self.view?.presentScene(newScene!, transition: reveal)
default:
break
}
}
所以我把它放在 override func didMoveToView(view: SKView)
中,但是一旦玩家跳起来 HUD 就会移动:
override func didMoveToView(view: SKView) {
....
// Build the HUD
// HUD
hudNode = SKNode()
hudNode.zPosition = 1000
addChild(hudNode)
// Coins
// 1
let coin = SKSpriteNode(imageNamed: "powerup05_1")
coin.position = CGPoint(x: 300, y: self.size.height-100)
coin.zPosition = 1000
hudNode.addChild(coin)
// 2
lblCoins = SKLabelNode(fontNamed: "ChalkboardSE-Bold")
lblCoins.fontSize = 70
lblCoins.fontColor = SKColor.whiteColor()
lblCoins.position = CGPoint(x: 375, y: self.size.height-100)
lblCoins.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Left
lblCoins.zPosition = 1000
// 3
lblCoins.text = String(format: "X %d", GameState.sharedInstance.coins)
hudNode.addChild(lblCoins)
// Score
// 4
lblScore = SKLabelNode(fontNamed: "ChalkboardSE-Bold")
lblScore.fontSize = 70
lblScore.fontColor = SKColor.whiteColor()
lblScore.position = CGPoint(x: self.size.width-325, y: self.size.height-100)
lblScore.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Right
lblScore.zPosition = 1000
// 5
lblScore.text = "0"
hudNode.addChild(lblScore)
}
将您的 hudNode
添加到 cameraNode
会使 HUD 与相机一起移动,以便它在屏幕上看起来是静态的。也就是把addChild(hudNode)
改成cameraNode.addChild(hudNode)
.
然后使用convert(_:to:)将点从当前场景坐标变为cameraNode
坐标。这将设置 HUD 在新坐标中的位置,只需稍作修改。例如,像这样更改 coin.position
:
coin.position = convert(CGPoint(x: 300, y: self.size.height-100), to: cameraNode)
对另一个 HUD 做同样的事情:
lblCoins.position = convert(CGPoint(x: 375, y: self.size.height-100), to: cameraNode)
lblScore.position = convert(CGPoint(x: self.size.width-325, y: self.size.height-100), to: cameraNode)
这4行修改后的结果:
我试图在本教程的跳跃游戏中创建一个静态 HUD,它在整个游戏过程中始终位于屏幕顶部。我对 Swift 还是个新手,所以也许这是我不容易看到的约定:
http://www.raywenderlich.com/87232/make-game-like-mega-jump-sprite-kit-swift-part-2
在教程中,他将HUD放在override init(size: CGSize)
函数中:
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(size: CGSize) {
super.init(size: size)
...
// Build the HUD
// HUD
hudNode = SKNode()
hudNode.zPosition = 1000
addChild(hudNode)
// Coins
// 1
let coin = SKSpriteNode(imageNamed: "powerup05_1")
coin.position = CGPoint(x: 300, y: self.size.height-100)
coin.zPosition = 1000
hudNode.addChild(coin)
// 2
lblCoins = SKLabelNode(fontNamed: "ChalkboardSE-Bold")
lblCoins.fontSize = 70
lblCoins.fontColor = SKColor.whiteColor()
lblCoins.position = CGPoint(x: 375, y: self.size.height-100)
lblCoins.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Left
lblCoins.zPosition = 1000
// 3
lblCoins.text = String(format: "X %d", GameState.sharedInstance.coins)
hudNode.addChild(lblCoins)
// Score
// 4
lblScore = SKLabelNode(fontNamed: "ChalkboardSE-Bold")
lblScore.fontSize = 70
lblScore.fontColor = SKColor.whiteColor()
lblScore.position = CGPoint(x: self.size.width-325, y: self.size.height-100)
lblScore.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Right
lblScore.zPosition = 1000
// 5
lblScore.text = "0"
hudNode.addChild(lblScore)
}
但是,如果我尝试在我的游戏中使用此功能,我会收到错误消息:
Argument labels '(fileNamed:)' do not match any available overloads
在这个函数中:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
switch gameState.currentState {
case is WaitingForTap:
gameState.enterState(WaitingForBomb)
// Switch to playing state
self.runAction(SKAction.waitForDuration(2.0),
completion:{
self.gameState.enterState(Playing)
})
case is GameOver:
// Error! Argument labels '(fileNamed:)' do not match any available overloads
let newScene = GameScene(fileNamed:"GameScene")
newScene!.scaleMode = .AspectFill
let reveal = SKTransition.flipHorizontalWithDuration(0.5)
self.view?.presentScene(newScene!, transition: reveal)
default:
break
}
}
所以我把它放在 override func didMoveToView(view: SKView)
中,但是一旦玩家跳起来 HUD 就会移动:
override func didMoveToView(view: SKView) {
....
// Build the HUD
// HUD
hudNode = SKNode()
hudNode.zPosition = 1000
addChild(hudNode)
// Coins
// 1
let coin = SKSpriteNode(imageNamed: "powerup05_1")
coin.position = CGPoint(x: 300, y: self.size.height-100)
coin.zPosition = 1000
hudNode.addChild(coin)
// 2
lblCoins = SKLabelNode(fontNamed: "ChalkboardSE-Bold")
lblCoins.fontSize = 70
lblCoins.fontColor = SKColor.whiteColor()
lblCoins.position = CGPoint(x: 375, y: self.size.height-100)
lblCoins.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Left
lblCoins.zPosition = 1000
// 3
lblCoins.text = String(format: "X %d", GameState.sharedInstance.coins)
hudNode.addChild(lblCoins)
// Score
// 4
lblScore = SKLabelNode(fontNamed: "ChalkboardSE-Bold")
lblScore.fontSize = 70
lblScore.fontColor = SKColor.whiteColor()
lblScore.position = CGPoint(x: self.size.width-325, y: self.size.height-100)
lblScore.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Right
lblScore.zPosition = 1000
// 5
lblScore.text = "0"
hudNode.addChild(lblScore)
}
将您的 hudNode
添加到 cameraNode
会使 HUD 与相机一起移动,以便它在屏幕上看起来是静态的。也就是把addChild(hudNode)
改成cameraNode.addChild(hudNode)
.
然后使用convert(_:to:)将点从当前场景坐标变为cameraNode
坐标。这将设置 HUD 在新坐标中的位置,只需稍作修改。例如,像这样更改 coin.position
:
coin.position = convert(CGPoint(x: 300, y: self.size.height-100), to: cameraNode)
对另一个 HUD 做同样的事情:
lblCoins.position = convert(CGPoint(x: 375, y: self.size.height-100), to: cameraNode)
lblScore.position = convert(CGPoint(x: self.size.width-325, y: self.size.height-100), to: cameraNode)
这4行修改后的结果: