【发布时间】:2018-04-26 06:53:29
【问题描述】:
我有一些看起来像这样的东西。其中“grid”是一个单独的绘制纹理的sksprite节点,初始化其他sprite,最后初始化一个touple列表。
class GameScene: SKScene {
var gameOver: Bool = false
let grid = Grid(blockSize: 15.0, rows:29, cols:28)
var direction: Int = IDLE
var totalSeconds:Int = 0
var balls: [(Int,Int)] = [(Int,Int)]()
let Car = SKSpriteNode(imageNamed: Car_IMAGE)
var Ghouls: [SKSpriteNode] = [SKSpriteNode]()
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
...
}
我遇到的问题是,当我过渡到游戏结束场景时,由更新球的数量是否达到零(事实上,它确实被触发)触发,我调用游戏结束函数并将游戏结束标志设置为 true,如下所示:
override func update(_ currentTime: TimeInterval) {
if self.gameOver == false {
// Called before each frame is rendered
print(Balls.count)
// update balls count if sprite touches it
self.checkForBalls()
// check if ghouls touched car
self.IfGameOver()
}
}
func IfGameOver() {
if self.balls.count == 0 {
goToScene(msg:"you lose")
balls.removeAll(keepingCapacity: false)
self.gameOver = true
}
}
func goToGameScene(msg: String){
let gameOverScene = GameOverScene(size: size)
gameOverScene.scaleMode = scaleMode
let reveal = SKTransition.flipHorizontal(withDuration: 0.5)
view?.presentScene(gameOverScene, transition: reveal)
}
一切都按预期进行,我能够从游戏结束场景转换回这个游戏场景。除了 int 元组的 balls 数组之外,所有实例变量都会重新初始化。我知道 bool 标志会重新初始化,因为最终我的终端再次开始打印计数,但我也知道我的数组没有重新初始化,因为打印到终端的计数正好是它最终达到的原始大小的两倍。
例如,如果我这样初始化数组:
func intializeBalls() {
// where OArray is just series of coordinates
for o in OArray {
balls.append(o.coordinate)
}
}
假设这个函数最终获得了球列表中 30 个附加项目的最终计数。下次游戏结束场景循环时,终端将在列表中打印 60 个球,这意味着最后一个项目从未被删除。
【问题讨论】:
标签: swift sprite-kit