返回App SpriteKit后按回放

时间:2016-04-15 21:55:19

标签: ios swift sprite-kit

  

APP DELEGATE CRASHING PROBLEM

我是编程的初学者,在开发我的应用程序时,出现了无法解决的问题。我试图在用户离开应用程序时暂停游戏,并且当需要返回时,由于按下主页按钮,游戏暂停。我的困难是,当用户离开应用程序时,游戏取消暂停并返回,它完美运行,但当用户暂停游戏并离开时,Xcode向我显示错误消息:“线程1:信号SIGARBT “游戏崩溃了。如果你看一下我的比赛,我会很高兴

  

CODE ITSELF

AppDelegate.swift

func applicationWillResignActive(application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

    //pause when leaving app
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("pauseState"), name: "pauseState", object: nil)
}

GameScene.swift

import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {

var gameLayer = SKNode()
var pauseLayer = SKNode()

var gameStarted = Bool()

var pauseButton = SKSpriteNode()
var playButton = SKSpriteNode()

func setupPauseButton(){

    //Pause
    pauseButton = SKSpriteNode (imageNamed: "pause")
    pauseButton.setScale(0.25)
    pauseButton.position = CGPoint(x: self.frame.width / 10, y: self.frame.height / 1.5)
    //pauseButton.zPosition = 3
}
func setupPlayButton(){

    //Play
    playButton = SKSpriteNode (imageNamed: "play")
    playButton.setScale(0.15)
    playButton.position = CGPoint(x: self.frame.width / 10, y: self.frame.height / 1.5)
    //playButton.zPosition = 3
}

//layers
func createGameLayer(){

    self.physicsWorld.contactDelegate = self

    setupPauseButton()
    gameLayer.addChild(pauseButton) //add pauseButton to gameLayer
    pauseButton.hidden = true
}
func createPauseLayer(){

    //Play
    setupPlayButton()
    pauseLayer.addChild(playButton) //add playButton to pauseLayer
}

override func didMoveToView(view: SKView) {
/* Setup your scene here */

    //pause when coming back
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("pauseState"), name: "pauseState", object: nil)

    self.addChild(gameLayer)

    createGameLayer()
    createPauseLayer()
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */

    //run only if game is running
    if gameLayer.paused == false{

        //pauseButton shows when starting to play
        pauseButton.hidden = false
    }

    //when touch buttons/screen
    for touch in touches{

        let location = touch.locationInNode(self)
        let node = nodeAtPoint(location)

        if node == pauseButton{
            pauseState()
        }

        else if node == playButton{
            playState()
        }

        else{

            if gameStarted == false{  //game didn't start yet

                gameStarted = true

                Character.physicsBody?.affectedByGravity = true //start falling when touch the screen

                //first jump
                Character.physicsBody?.velocity = CGVectorMake(0, 0)
                Character.physicsBody?.applyImpulse(CGVectorMake(0, 15))
                }
            }
            else{

                //jump
                Character.physicsBody?.velocity = CGVectorMake(0, 0)
                Character.physicsBody?.applyImpulse(CGVectorMake(0, 15))
                }
            }
        }
    }
}

//states
func pauseState(){

    pauseButton.hidden = true //hide pauseButton
    self.physicsWorld.speed = 0 //pause physics (character, etc)
    gameLayer.paused = true //pause gameLayer

    self.addChild(pauseLayer) //add pauseLayer
}
func playState(){

    pauseLayer.removeFromParent() //remove pauseLayer

    gameLayer.paused = false //unpause gameLayer
    self.physicsWorld.speed = 1 //unpause physics (character, etc)
    pauseButton.hidden = false //show pauseButton
}

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
}

}

1 个答案:

答案 0 :(得分:1)

我注意到你的代码中的两件事

1)从swift 2.2开始,apple终于将选择器方法从字符串中删除了。所以在你的gameScene中将观察者改为此。

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(GameScene.pauseState), name: "pauseState", object: nil)

https://medium.com/swift-programming/swift-selector-syntax-sugar-81c8a8b10df3#.kncj5thdh

2)在您的app委托中,您正在添加另一个没有意义的观察者。您想将通知发布给GameScene中的观察者。

将其更改为此

NSNotificationCenter.defaultCenter().postNotificationName("pauseState", object: nil)

3)不确定你是否这样做,但总是建议你在离开场景时删除任何NSNotification观察者。请拨打此行

NSNotificationCenter.defaultCenter().removeObserver(self)

当您转换到新场景或GameScenes&#34; WillMoveFromView&#34;方法

如果你遵循苹果命名惯例,这也是一个好主意。只有类,结构,枚举和协议应以大写字母开头。这使您的代码对您自己和SO更具可读性(您的代码标记为蓝色但不应该

希望这有帮助。