TVOS:检测按下开始的触摸和功能(Swift Spritekit)

问题描述 投票:0回答:2

我试图在 TVOS 中定义触摸,但它不起作用。

我想连接3个功能

  • 开始游戏
  • 播放暂停音乐
  • 重新开始游戏

游戏场景TVOS:

 func StartGameRecognizer(gesture: UITapGestureRecognizer) {

        if isGameOver {
        } else if !isStarted {
            start()
        } else {
            hero.flip()
        }

    }

 func playPauseMusicRecognizer(gesture: UITapGestureRecognizer) {

            let onoroff = UserDefaults.standard.bool(forKey: "onoroff")

            if !onoroff { //playing is false
                Singleton.sharedInstance().pauseBackgroundMusic()
                SoundOnOff.texture = SKTexture(imageNamed:"Sound-off.png")
                UserDefaults.standard.set(true, forKey: "onoroff")
            }
            else {
                Singleton.sharedInstance().resumeBackgroundMusic()
                SoundOnOff.texture = SKTexture(imageNamed:"Sound-on.png")
                UserDefaults.standard.set(false, forKey: "onoroff")
            }
        }

func RestartGameRecognizer(gesture: UISwipeGestureRecognizer){
    print("RestartGame")
    //Re-open GameScene

    GameViewController().TitleGameOver.isHidden = true
    GameViewController().RestartButton.isHidden = true
    GameViewController().scoreTextLabel.isHidden = true
    GameViewController().highscoreTextLabel.isHidden = true
    GameViewController().ScoreBoardTV.isHidden = true
    GameViewController().Score.isHidden = true
    GameViewController().HighScore.isHidden = true
    GameViewController().NewhighscoreTextLabel.isHidden = true
    GameViewController().HomeButton.isHidden = true
    // Singleton.sharedInstance().resumeSoundEffectClickedButton()
    GameViewController().gameDidStart()


}

游戏视图控制器TVOS:

 override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
    for press in presses {
        switch press.type {
        case .upArrow:
            print("Up Arrow")
        case .downArrow:
            print("Down arrow")
        case .leftArrow:
            print("Left arrow")
        case .rightArrow:
            print("Right arrow")
        case .select:
            print("Select")
        case .menu:
            print("Menu")
        case .playPause:
            print("Play/Pause")

        default:
        print("")
        }
    }
}
  • 如何正确使用它?
  • 如何在场景和视图控制器之间传递功能?

我需要示例或提示才能正确编写代码。

更新:

GameSceneTvOS:

    override func didMove(to view: SKView) {

        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(GameSceneTVOS.StartGameRecognizer(gesture:)))
        tapGesture.allowedPressTypes = [NSNumber(value: UIPressType.Select.rawValue)]
        view.addGestureRecognizer(tapGesture)

        let tapGesture1 = UITapGestureRecognizer(target: self, action: #selector(GameSceneTVOS.PlaypauseMusicRecognizer(gesture:)))
        tapGesture1.allowedPressTypes = [NSNumber(value: UIPressType.PlayPause.rawValue)]
        view.addGestureRecognizer(tapGesture1)

        let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(GameSceneTVOS.RestartGameRecognizer(gesture:)))
        swipeUp.direction = UISwipeGestureRecognizerDirection.up
        self.view?.addGestureRecognizer(swipeUp)
}

**Functions :**

func StartGameRecognizer(gesture: UITapGestureRecognizer) {
        print("StartGame")

            if isGameOver {
                } else if !isStarted {
                    start()
                } else {
                    hero.flip()
                }

            }

    func PlaypauseMusicRecognizer(gesture: UITapGestureRecognizer) {
        print("PlaypauseMusic")


       let onoroff = UserDefaults.standard.bool(forKey: "onoroff")

        if !onoroff { //playing is false
            Singleton.sharedInstance().pauseBackgroundMusic()
            SoundOnOff.texture = SKTexture(imageNamed:"Sound-off.png")
            UserDefaults.standard.set(true, forKey: "onoroff")
        }
        else {
            Singleton.sharedInstance().resumeBackgroundMusic()
            SoundOnOff.texture = SKTexture(imageNamed:"Sound-on.png")
            UserDefaults.standard.set(false, forKey: "onoroff")
        }
    }


    func RestartGameRecognizer(gesture: UISwipeGestureRecognizer){
        print("RestartGame")
        //Re-open GameScene

        GameViewController().TitleGameOver.isHidden = true
        GameViewController().RestartButton.isHidden = true
        GameViewController().scoreTextLabel.isHidden = true
        GameViewController().highscoreTextLabel.isHidden = true
        GameViewController().ScoreBoardTV.isHidden = true
        GameViewController().Score.isHidden = true
        GameViewController().HighScore.isHidden = true
        GameViewController().NewhighscoreTextLabel.isHidden = true
        GameViewController().HomeButton.isHidden = true
        // Singleton.sharedInstance().resumeSoundEffectClickedButton()
        GameViewController().gameDidStart()


    }

sprite-kit swift3 tvos
2个回答
2
投票

您的代码有一些问题。

1)restartGame方法中这段代码是错误的。

  GameViewController().TitleGameOver.isHidden = true
  GameViewController().RestartButton.isHidden = true
  ...

您正在每一行创建 GameViewController 的新实例,而不是引用当前的游戏视图控制器。

2) 您不应该使用 GameViewController 来创建 UI,您应该仅使用 SpriteKit API(SKLabelNodes、SKSpriteNodes、SKNodes 等)直接在相关的 SKScenes 中执行此操作。在 SpriteKit 中使用 UIKit,除非在某些情况下,否则是不好的做法。

3) 您应该直接在 SKScenes 中使用 TouchesBegan、TouchesMoved 等来获取触摸输入,不要使用 GameViewController 方法。 它们会充满火,就像您在 iOS 上一样。

您还可以在 SKScene 中创建手势识别器,以从 SiriRemote 获取按钮按下操作。

/// Pressed, not tapped, main touch pad
let pressedMain = UITapGestureRecognizer(target: self, action: #selector(SOMEMETHOD))
pressedMain.allowedPressTypes = [NSNumber(value: UIPressType.select.rawValue)]
view?.addGestureRecognizer(pressedMain)

/// Pressed play pause button
let pressedPlayPause = UITapGestureRecognizer(target: self, action: #selector(SOMEMETHOD))
pressedPlayPause.allowedPressTypes = [NSNumber(value: UIPressType.playPause.rawValue)]
view?.addGestureRecognizer(pressedPlayPause)

 /// Pressed menu button
let pressedMenu = UITapGestureRecognizer(target: self, action: #selector(SOMEMETHOD))
pressedMenu.allowedPressTypes = [NSNumber(value: UIPressType.menu.rawValue)]
view?.addGestureRecognizer(pressedMenu)

如果需要,您还可以使用滑动手势识别器

let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(SOMEMETHOD))
rightSwipe.direction = .right
view?.addGestureRecognizer(rightSwipe)

let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(SOMEMETHOD))
leftSwipe.direction = .left
view?.addGestureRecognizer(leftSwipe)

let upSwipe = UISwipeGestureRecognizer(target: self, action: #selector(SOMEMETHOD))
upSwipe.direction = .up
view?.addGestureRecognizer(upSwipe)

let downSwipe = UISwipeGestureRecognizer(target: self, action: #selector(SOMEMETHOD))
downSwipe.direction = .down
view?.addGestureRecognizer(downSwipe)

如果您正在使用手势识别器,请记住它们已添加到 GameViewController (view?.addGesture...),因此在添加新手势识别器或更改到可能需要的新场景时删除它们是一个很好的做法不同的。

退出场景或添加新的手势识别器时调用此代码。

 for gestureRecognizer in view?.gestureRecognizers ?? [] {
        view?.removeGestureRecognizer(gestureRecognizer)
    }

如果您正在寻找成熟的微型游戏手柄支持,那么您将需要观看一些有关游戏控制器框架的教程。

4)尝试将字符串键(例如 UserDefaults 的字符串键)放入某些属性中。

 enum Key: String {
     case onoroff
 }

然后像这样使用它

 UserDefaults.standard.set(true, forKey: Key.onoroff.rawValue)

以避免拼写错误。

5)您应该始终遵循 Swift 约定,您的某些方法和属性以大写字母开头,但它们不应该。

我建议您重新构建代码,而不是继续尝试使用 GameViewController 来完成所有这些操作。这一切都应该直接在相关的 SKScene 中完成。

编辑。我认为你调用选择器是错误的,试试这个。当你的函数有一个参数时,你会使用这个(_:),你正在尝试使用(手势:)。试试这个吧。

 ... action: #selector(startGameRecognizer(_:))

希望这有帮助


0
投票

请勿使用 UISwipeGestureRecognizer 或 UITapGestureRecognizer 手势。

利用presses开始调用dPadUp、dpadDown、aAction、bAction等中的函数

extension GameViewController {
    
    // MARK: - tvOS Controls -

    override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {

        var didHandleEvent = false
        
        for press in presses {
            
            #if os(tvOS)
            
            // tvOS
            
            if press.type == .select {
                
                // click touch pad
                
                print("select")
                
                self.aAction()
                
            } else if press.type == .menu {
                
                // menu button
                
                print("menu")
                
            } else if press.type == .playPause {
                
                // play pause
                
                self.bAction()
                
                print("playPause")
                
            } else if press.type == .upArrow {
                
                // upArrow
                
                print("upArrow")
                
                self.dPadUp()
                
            } else if press.type == .leftArrow {
                
                // leftArrow
                
                print("leftArrow")
                
                self.dPadLeft()
                
            } else if press.type == .downArrow {
                
                // downArrow
                
                print("downArrow")
                
                self.dPadDown()
                
            } else if press.type == .rightArrow {
                
                // rightArrow
                
                print("rightArrow")
                
                self.dPadRight()
                
            }
                
            if #available(tvOS 14.3, *) {

                if press.type == .pageUp {
                    
                    // pageUp
                    
                    print("pageUp")
                    
                } else if press.type == .pageDown {
                    
                    // pageDown
                    
                    print("pageDown")
                    
                }
                
            }
            
            #endif

        }

        if didHandleEvent == false {
            
            // Didn't handle this key press, so pass the event to the next responder.

            super.pressesBegan(presses, with: event)

        }

    }
    
    // MARK: - Keyboard -
    
    @objc func dPadUp() {
        
        print("up")
                
    }
    
    @objc func dPadLeft() {
        
        print("left")
        
    }
    
    @objc func dPadRight() {
        
        print("right")
        
    }
    
    @objc func dPadDown() {
        
        print("down")
                        
    }
    
    @objc func idleButton() {
        
        print("idle")
                
    }
    
    @objc func aAction() {
        
        print("aAction")
                
    }
    
    @objc func xAction() {
        
        print("xAction")
                
    }
    
    @objc func yAction() {
        
        print("yAction")
                
    }

    @objc func bAction() {
        
        print("bAction")
                        
    }
        
}
© www.soinside.com 2019 - 2024. All rights reserved.