【问题标题】:TVOS : detecting touches with press began and functions (Swift Spritekit)TVOS:通过按下开始和功能检测触摸(Swift Spritekit)
【发布时间】:2016-10-03 12:09:53
【问题描述】:

我试图在 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()


}

GameViewControllerTVOS:

 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


    【解决方案1】:

    你的代码有问题。

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

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

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

    2) 你不应该使用 GameViewController 来创建你的 UI,你应该只使用 SpriteKit API(SKLabelNodes、SKSpriteNodes、SKNodes 等)直接在相关的 SKScene 中进行。除了在某些情况下,在 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)
        }
    

    如果您正在寻找完全成熟的微型游戏手柄支持,则需要观看一些有关 gameController 框架的教程。

    4) 尝试将您的字符串键与 UserDefaults 的字符串键一样放在某些属性中。

     enum Key: String {
         case onoroff
     }
    

    而不是像这样使用它

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

    为了避免拼写错误。

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

    我建议您重新构建代码,不要继续尝试使用 GameViewController 进行所有这些操作。应该全部直接在相关的SKScene中完成。

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

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

    希望对你有帮助

    【讨论】:

    • 我无法定义选择和播放/暂停它不起作用
    • 您需要在场景中的 DidMoveToView 中添加此项。
    • 这是我所做的,它在控制台中也没有响应
    • 您应该退后一步,先修复您的代码。您的示例代码绝对不能用于生产。这包括删除您在 GameViewController 中除默认代码之外的所有内容。首先在您的 SKScenes 中重新创建您的 UI,然后再尝试使用电视控件的手势。
    • 我做到了,只是在模拟器上滑动不工作,点击工作!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-04
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    相关资源
    最近更新 更多