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

Posted

技术标签:

【中文标题】除非模拟器是 XS 类型,否则屏幕上的按钮不起作用【英文标题】:Buttons on screen do not work unless simulator is of the XS variety 【发布时间】:2018-12-26 19:28:30 【问题描述】:

基本上我的应用程序运行良好,但是当我尝试在 Xcode 中的 XS 模拟器以外的任何其他模拟器上运行它时,主菜单按钮不再起作用,而且我没有去哪里寻找错误,因为什么也没有正在抛出任何错误。此外,当我将打印语句放在节点内时,打印语句会打印到控制台,告诉我屏幕正在检测选择,但由于某种原因,代码无法运行。这很奇怪,因为它是被调用函数中的一个简单 if 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()

【问题讨论】:

顺便说一句,在“普通”ios 中简单地执行按钮/等操作非常常见,并且仅使用 SKScene 显示游戏本身。 等待 - 一般来说 spriteKit/Metal 等在模拟器中不起作用!你必须建立一个电话!它可能只是在新的 XS 模拟器中“巧合”地工作。就是这样。仅适用于手机。 我没有使用任何自动布局约束。此外,我知道这些按钮正在被选中,因为当我触摸它们时我正在打印到控制台,并且可以看到它正在检测选择。 对@Alec,答案在下面!很简单... 有趣。所以我可以放心,它适用于任何 iPhone 型号?对不起,如果我听起来很密集。 【参考方案1】:

一般来说 spriteKit/Metal 等在模拟器中不起作用!

你必须构建到手机!

它可能只是“巧合地”在新的 XS 模拟器中工作。就是这样。仅适用于手机。

【讨论】:

以上是关于除非模拟器是 XS 类型,否则屏幕上的按钮不起作用的主要内容,如果未能解决你的问题,请参考以下文章

除非您离开应用程序并返回,否则按钮 onclick 事件在 iOS 中不起作用,然后它就起作用了

主页和后退按钮在 android 模拟器像素 2 和像素 3 api 中不起作用

在顺风 css 示例中设置单选按钮样式不起作用

除非我刷新,否则 jQuery mobile 中的 JavaScript 不起作用

除非它等于0,否则声明一个变量不起作用[重复]

按钮在 ipad 模拟器上不起作用