如果monkey跟敌人相撞,或落出屏幕的话,就会显示”Game Over“,接着出现重新点击开始游戏的画面。
首先将以下所有变量添加到GameScene顶端,在 let player = SKSpriteNode(imageNamed:”spacemonkey_fly02”)之后:
// 1 var gameOver = false // 2 let endLabel = SKLabelNode(text: "Game
Over") let endLabel2 = SKLabelNode(text: "Tap to restart!") let touchToBeginLabel
= SKLabelNode(text: "Touch to begin!") let points = SKLabelNode(text: "0")
// 3 var numPoints = 0 // 4 let explosionSound = SKAction.playSoundFileNamed("explosion.mp3",
waitForCompletion: true) let coinSound = SKAction.playSoundFileNamed("coin.wav",
waitForCompletion: false)
再回顾一下上述步骤:
下一步创建名为setupLabels()的新方法:
func setupLabels() {
// 1
touchToBeginLabel.position = CGPoint(x: frame.size.width/2, y: frame.size.height/2)
touchToBeginLabel.fontColor = UIColor.whiteColor()
touchToBeginLabel.fontSize = 50
addChild(touchToBeginLabel)
// 2
points.position = CGPoint(x: frame.size.width/2, y: frame.size.height * 0.1)
points.fontColor = UIColor.whiteColor()
points.fontSize = 100
addChild(points)
}
再回顾一下上述步骤:
现在在didMoveToView(_:)里调用setupLabels():
setupLabels()
再删掉touchesBegan(_:withEvent:),添加如下代码:
override func touchesBegan(touches: Set, withEvent event: UIEvent) {
// 1
if (!gameOver) {
if player.physicsBody?.dynamic == false {
player.physicsBody?.dynamic = true
touchToBeginLabel.hidden = true
backgroundColor = SKColor.blackColor()
runAction(SKAction.repeatActionForever(
SKAction.sequence([
SKAction.runBlock(spawnEnemy),
SKAction.waitForDuration(1.0)])))
}
// 2
jumpPlayer()
}
// 3
else if (gameOver) {
let newScene = GameScene(size: size)
newScene.scaleMode = scaleMode
let reveal = SKTransition.flipHorizontalWithDuration(0.5)
view?.presentScene(newScene, transition: reveal)
}
}
回顾一下:
override func update(currentTime: CFTimeInterval) {
//1
if !gameOver {
//2
if player.position.y <= 0 {
endGame()
}
//3
enumerateChildNodesWithName("enemy") {
enemy, _ in
//4
if enemy.position.x <= 0 {
//5
self.updateEnemy(enemy)
}
}
}
}
回顾一下:
现在添加名为updateEnemy()的method,在框架渲染前调用——产生的效果是:每当一个敌人消失,玩家就会得1分:
func updateEnemy(enemy: SKNode) { //1 if enemy.position.x < 0 { //2
enemy.removeFromParent() //3 runAction(coinSound) //4 numPoints++ //5 points.text
= "\(numPoints)" } }
再回顾一下上述步骤:
现在需要稍稍改变下didBeginContact(_:)了。从parent移除第一个节点之后,添加这行代码:
endGame()
现在终于可以用endGamemethod了,久等了:
func endGame() {
// 1
gameOver = true
// 2
removeAllActions()
// 3
runAction(explosionSound)
// 4
endLabel.position = CGPoint(x: frame.size.width/2, y: frame.size.height/2)
endLabel.fontColor = UIColor.whiteColor()
endLabel.fontSize = 50
endLabel2.position = CGPoint(x: frame.size.width/2, y: frame.size.height/2 + endLabel.fontSize)
endLabel2.fontColor = UIColor.whiteColor()
endLabel2.fontSize = 20
points.fontColor = UIColor.whiteColor()
addChild(endLabel)
addChild(endLabel2)
}
现在来回顾一下:
现在用didMoveToView(_:)移除这个代码块:
ackgroundColor = SKColor.blackColor()
runAction(SKAction.repeatActionForever(
SKAction.sequence([
SKAction.runBlock(spawnEnemy),
SKAction.waitForDuration(1.0)])))
最后添加一行:
player.physicsBody?.dynamic = false
这样设置之后,点击屏幕后游戏才会开始,否则monkey是不会移动的。
编写完成后运行,游戏大功告成了!
等会儿,还有一件事!打开ViewController.swift,添加一个新属性:
var backgroundMusicPlayer: AVAudioPlayer!
还要添加一个新method:
func playBackgroundMusic(filename: String) {
let url = NSBundle.mainBundle().URLForResource(
filename, withExtension: nil)
if (url == nil) {
println("Could not find file: \(filename)")
return
}
var error: NSError? = nil
backgroundMusicPlayer =
AVAudioPlayer(contentsOfURL: url, error: &error)
if backgroundMusicPlayer == nil {
println("Could not create audio player: \(error!)")
return
}
backgroundMusicPlayer.numberOfLoops = -1
backgroundMusicPlayer.prepareToPlay()
backgroundMusicPlayer.play()
}
这个method很方便,用来添加一些背景音乐。工作原理在此不详述。
用起来很简单,只要在skView.presentScene(scene)那一行后添加viewWillLayoutSubviews():
playBackgroundMusic("BackgroundMusic.mp3")
编写好之后运行,美妙的背景音乐出现啦!太棒了!
转自:http://www.csdn.net/article/2015-05-26/2824772-sprite-kit-for-kids-with-swift/4