Swift SpriteKit出现了一个错误

时间:2016-06-04 04:40:26

标签: swift sprite-kit

我正在尝试制作一个小行星类型的游戏,并且每当小行星与玩家碰撞时,结束场景加载得很好,当我尝试重新启动它时切割,就像代码被卡住循环调用结束场景的碰撞功能一样。这是碰撞功能

func didBeginContact(contact: SKPhysicsContact) {
    print("5")
    //Creating a variable that holds which two items collide
    let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
    //Creating a switch case statement
    //This will run a different case depending on whitch two objects collide
    switch(contactMask) {
    //If the satellite and the asteroid collide do this
    case bitMask.satellite.rawValue | bitMask.asteroid.rawValue:
        //removing the satellite
        let secondNode = contact.bodyB.node
        secondNode?.physicsBody?.allowsRotation = true
        let firstNode = contact.bodyA.node
        firstNode?.removeFromParent()
        //Stopping the mission timer
        removeActionForKey("missionDurationTime")
        self.removeAllChildren()
        print("6")
        self.view?.presentScene(EndScene())
        print("7")
    default:
        return
    }
}

这是从我的最终场景回到我的游戏场景的功能

func Restart(){
    print("hello")
    RestartBtn.removeFromSuperview()
    speach.removeFromSuperview()
    score.removeFromSuperview()
    gameOver.removeFromSuperview()
    self.view?.presentScene(GameScene())

}

点击重启按钮后,EndScene中的所有标签都会消失,但屏幕显示为空白,控制台只显示“5”  6  7 计算的次数太多,直到最终场景中的标签返回。我非常困惑,在我头上。非常感谢任何帮助!

更新:

我将重启按钮中的代码更改为此

print("hello")
    RestartBtn.removeFromSuperview()
    speach.removeFromSuperview()
    score.removeFromSuperview()
    gameOver.removeFromSuperview()
    let scene =  GameScene(size: self.view!.bounds.size)
    scene.backgroundColor = SKColor(red: 1, green: 1, blue: 1, alpha: 1)
    scene.scaleMode = .AspectFill
    scene.backgroundColor = UIColor.whiteColor()
    let transition = SKTransition.pushWithDirection(SKTransitionDirection.Left, duration:0.5)
    self.scene?.view?.presentScene(scene, transition: transition)

现在游戏场景加载,一切都应该如此,除了事实上一切都要大五倍。它应该是这样的: What it should look like

这就是它的样子 What it does look like

1 个答案:

答案 0 :(得分:0)

在重新启动代码时,您正在使用view.bounds的大小初始化GameScene。

如果您没有更改GameViewController代码,则默认使用GameScene.sks文件作为参考(可视级别编辑器),默认场景大小为1024 * 768,缩放模式为.AspectFill。

简而言之,在重启功能中试试这个

 if let scene =  GameScene(fileNamed: "GameScene") { // fileNamed is the GameScene.sks file
     scene.backgroundColor = SKColor(red: 1, green: 1, blue: 1, alpha: 1) 
      ....


 }

或者

 let scene = GameScene(size: CGSize(width: 1024, height: 768)
 scene.backgroundColor ....