除非模拟器是XS品种,否则屏幕上的按钮不起作用

时间:2018-12-26 19:28:31

标签: ios swift nodes compatibility skspritenode

基本上,我的应用程序运行良好,但是当我尝试在Xcode中的XS模拟器以外的任何其他模拟器上运行该应用程序时,主菜单按钮将不再起作用,并且我也无处可寻,因为没有任何错误抛出任何错误。此外,当我将打印语句放入节点内时,该打印语句将打印到控制台,该控制台告诉我屏幕正在检测选择,但是由于某种原因该代码无法运行。这很奇怪,因为在函数内部调用else语句很简单,因此我认为没有理由不应该运行它。

 var background = SKSpriteNode(imageNamed: "bgImage")
var newGameButtonNode:SKSpriteNode!
var difficultyButtonNode:SKSpriteNode!
var difficultyLabelNode:SKLabelNode!
var gameModeNode:SKSpriteNode!
var gameModeLabel:SKLabelNode!
var gameMode:Int = 1


override func didMove(to view: SKView) {

    background.zPosition = -4
    background.position = CGPoint(x: frame.size.width / 2, y: frame.size.height / 2)
    background.scale(to: (self.scene?.size)!)
    addChild(background)
    newGameButtonNode = self.childNode(withName: "newGame") as? SKSpriteNode
    difficultyButtonNode = self.childNode(withName: "difficultyButton") as? SKSpriteNode
    difficultyLabelNode = self.childNode(withName: "difficultyLabel") as? SKLabelNode
    gameModeNode = SKSpriteNode(imageNamed: "toggle")
    gameModeLabel = self.childNode(withName: "gameModeLabel") as! SKLabelNode

    let userDefaults = UserDefaults.standard
    if userDefaults.bool(forKey: "hard") {
        difficultyLabelNode.text = "HARD MODE"
    } else {
        difficultyLabelNode.text = "EASY MODE"
    }
    gameMode = userDefaults.integer(forKey: "mode")
    if gameMode == 1{
        gameModeLabel.text = "Crypto Catch"
        userDefaults.set(1, forKey: "mode")
    } else if gameMode == 2 {
        gameModeLabel.text = "Crypto Invaders"
        userDefaults.set(2, forKey: "mode")
    } else if gameMode == 3 {
        gameModeLabel.text = "Crypto Galiga"
        userDefaults.set(3, forKey: "mode")
    }

    difficultyButtonNode.texture = SKTexture(imageNamed: "toggleYellow")
    difficultyButtonNode.anchorPoint = CGPoint(x: 0, y: 0)
    difficultyButtonNode.scale(to: CGSize(width: 250 , height: 250))
    newGameButtonNode.texture = SKTexture(imageNamed:"playGameYellow")
    newGameButtonNode.anchorPoint = CGPoint(x: 0, y: 0)
    gameModeNode.anchorPoint = CGPoint(x: 0, y: 0)
    gameModeNode.scale(to: CGSize(width: 250, height: 250))
    gameModeNode.name = "gameModeNode"
    newGameButtonNode.scale(to: CGSize(width: 250, height: 250))
    difficultyButtonNode.position =  CGPoint(x: (scene?.size.width)!/2-125, y: (scene?.size.height)!/2-150)
    newGameButtonNode.position = CGPoint(x: (scene?.size.width)!/2-125, y:  (scene?.size.height)!/2+150)
    difficultyLabelNode.position = CGPoint(x: (scene?.size.width)!/2, y: (scene?.size.height)!/2-175)
    gameModeLabel.position = CGPoint(x: (scene?.size.width)!/2, y: (scene?.size.height)!/2-175 - 300)
    self.addChild(gameModeNode)
    gameModeNode.position = CGPoint(x: (scene?.size.width)!/2-125, y: (scene?.size.height)!/2-175 - 250)


}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first
    if let location = touch?.location(in: self){
        let nodesArray = self.nodes(at: location)
        if nodesArray.first?.name == "newGame"{
            if gameMode == 1{
                let transition = SKTransition.flipHorizontal(withDuration: 0.5)
                let gameSceneOB = GameScene(fileNamed: "GameScene")
                gameSceneOB?.scaleMode = .aspectFill

                self.view?.presentScene(gameSceneOB!, transition: transition)
            } else if gameMode == 2 {
                let transition = SKTransition.flipHorizontal(withDuration: 0.5)
                let gameSceneOB = GameScene(fileNamed: "ShooterGame")
                gameSceneOB?.scaleMode = .aspectFill

                self.view?.presentScene(gameSceneOB!, transition: transition)

            } else if gameMode == 3 {
                let transition = SKTransition.flipHorizontal(withDuration: 0.5)
                let gameSceneOB = GameScene(fileNamed: "cryptoGal")
                gameSceneOB?.scaleMode = .aspectFill

                self.view?.presentScene(gameSceneOB!, transition: transition)

            }

        } else if nodesArray.first?.name == "difficultyButton" {
            changeDifficulty()
        } else if nodesArray.first?.name == "gameModeNode"{
            toggleGameMode()

        }
    }
}
func changeDifficulty() {
    let userDefaults = UserDefaults.standard
    if difficultyLabelNode.text  == "EASY MODE"{
        difficultyLabelNode.text = "HARD MODE"
        userDefaults.set(true, forKey: "hard")
    } else {
        difficultyLabelNode.text = "EASY MODE"
        userDefaults.set(false, forKey: "hard")
    }
    userDefaults.synchronize()
}
func changeGameMode() {
    let userDefaults = UserDefaults.standard
    if gameMode == 1{
        gameModeLabel.text = "Crypto Catch"
        userDefaults.set(1, forKey: "mode")
    } else if gameMode == 2 {
        gameModeLabel.text = "Crypto Invaders"
        userDefaults.set(2, forKey: "mode")
    } else if gameMode == 3 {
        gameModeLabel.text = "Crypto Galiga"
        userDefaults.set(3, forKey: "mode")
    }
    userDefaults.synchronize()
}
func toggleGameMode(){
    if gameMode == 1{
        gameMode = 2
    } else if gameMode == 2 {
        gameMode = 3
    } else if gameMode == 3 {
        gameMode = 1
    }
    changeGameMode()
}

1 个答案:

答案 0 :(得分:2)

通常来说spriteKit / Metal等在模拟器中根本不起作用!

您必须构建手机!

在新的XS模拟器中,它可能只能“巧合”地工作。仅此而已。仅针对手机构建。