我的游戏中有以下菜单场景层次结构: GameLogoScene-> MainMenuScene(点击播放) - > GameLevelScene->(点击level1) - > GamePlayScene
第一个场景(GameLogoScene)在GameViewController类中设置如下:
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
let skView = self.view as? SKView
if skView?.scene == nil {
skView?.showsFPS = true
skView?.showsNodeCount = true
skView?.showsPhysics = true
skView?.ignoresSiblingOrder = false
let gameLogoScene = GameLogoScene(size: skView!.bounds.size)
gameLogoScene.scaleMode = .AspectFill
skView?.presentScene(gameLogoScene)
}
}
在GameLogoScene中,我使用以下代码切换到下一个场景(MainMenuScene):
let mainMenuScene = MainMenuScene(size: self.size)
self.view?.presentScene(mainMenuScene)
对GameLevelScene进行了类似的更改。然后在GameLevelScene中,我以下列方式切换到GamePlayScene:
let gamePlayScene = GamePlayScene(size: self.size, levelName:.level1)
self.view?.presentScene(gamePlayScene)
当满足获胜条件时,可以通过以下方式切换到GameOverScene:
if hasWon {
self.removeAllChildren()
self.removeAllActions()
let gameOverScene = GameOverScene(size: self.frame.size, playerWon: true, score:score.currentScore, currentLevel:currentLevel)
scene?.view?.presentScene(gameOverScene)
}
在GameOverScene中,下一级按钮假设重新加载GamePlayScene,但不是以新级别重新加载或重置场景,刚刚完成的级别的游戏玩法继续。请参阅以下代码:
if nextLevelButton.containsPoint(location){
let nextGameLevelScene = GamePlayScene(size: self.size, levelName:.level2)
self.view?.presentScene(nextGameLevelScene)
}
导致此行为的原因是什么?如何解决?
更新
以下是GamePlayScene的相关代码:
class GamePlayScene: SKScene, SKPhysicsContactDelegate {
init(size: CGSize, levelName: LevelName ) {
super.init(size: size)
currentLevel = levelName
switch levelName {
case .level1:
print("level1")
var spriteSpeed = 1
createMultiSprites([sSprite, mSprite, sSprite, sSprite, mSprite, lSprite, xlSprite], positions: [0, 1, 2, 3, 5, 7, 8])
createMultiEnemies([lEnemySprite, lEnemySprite, xlEnemySprite], positions: [0,3, 5])
case .level2:
var spriteSpeed = 2
print("level2")
createMultiSprites([xlSprite, xlSprite, xlSprite], positions: [5, 6, 7])
createMultiEnemies([lEnemySprite, lEnemySprite, xlEnemySprite, xlEnemySprite ], positions: [0, 3, 5, 7])
case .level3:
var spriteSpeed = 3
print("level3")
createMultiSprites([mSprite, xlSprite, mSprite, mSprite, mSprite], positions: [0, 1, 2, 3, 5])
createMultiEnemies([xlEnemySprite, xlEnemySprite, xxlEnemySprite], positions: [0,3, 5])
}
movement()
}
}
此外,我的sSprite,..,xxlSprite和sEnemySprite,...,xxlEnemySprite不会从SKSpriteNode继承,但具有SKSpriteNode属性。例如,sSprite的SKSpriteNode是sSprite.sprite。