【问题标题】:TVOS 10 SpriteKit Focus Navigation default focused itemTVOS 10 SpriteKit 焦点导航默认焦点项目
【发布时间】:2016-09-08 15:41:29
【问题描述】:

我正在尝试更新我的 SpriteKit 游戏以使用新的 SKNode 焦点导航功能,但我无法更改默认的焦点项目。

基本上我的按钮类中有这段代码来支持焦点导航

class Button: SKSpriteNode {

   var isFocusable = true // easy way to disable focus incase menus are shown etc

   required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        userInteractionEnabled = true
   } 

// MARK: - Focus navigation

#if os(tvOS)
extension Button {

    /// Can become focused
    override var canBecomeFocused: Bool {
        return isFocusable
    }

    /// Did update focus
    override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {

        if context.previouslyFocusedItem === self {
            // some SKAction to reset the button to default settings  
        }

        else if context.nextFocusedItem === self {
            // some SKAction to scale the button up 
        }
    }
} 
#endif     

一切都很好,但默认情况下,屏幕左侧的第一个按钮是焦点。

我正在尝试将其更改为另一个按钮,但我无法做到。我现在你应该使用

 override var preferredFocusEnvironments: [UIFocusEnvironment]...

 // preferredFocusedView is depreciated

但我不明白如何以及在哪里使用它。

我尝试将此代码添加到我的菜单场景中,以将焦点按钮从默认(商店按钮)更改为屏幕右侧的播放按钮。

class MenuScene: SKScene {

     // left side of screen
     lazy var shopButton: Button = self.childNode(withName: "shopButton")

    // right side of screen
    lazy var playButton: Button = self.childNode(withName: "playButton")

    // Set preferred focus 
    open override var preferredFocusEnvironments: [UIFocusEnvironment] {
       return [self.playButton]
    }
}

并调用

setNeedsFocusUpdate()
updateFocusIfNeeded()

在 didMoveToView 中,但它不起作用。

如何更改 SpriteKit 中的默认焦点按钮?

【问题讨论】:

  • 我的问题似乎和你的很相似,但我看不出我做错了什么。请查看stackoverflow.com/questions/41712256/…
  • 你好,我稍后看看。您是否按照我的答案和我发布的链接中的所有步骤进行操作?第一眼看你的代码不太确定哪里出了问题。

标签: swift sprite-kit focus tvos


【解决方案1】:

多亏了这篇精彩的文章,我终于得到了这个工作。

https://medium.com/folded-plane/tvos-10-getting-started-with-spritekit-and-focus-engine-53d8ef3b34f3#.pfwcw4u70

我完全错过的主要步骤是你告诉你的 GameViewController 你的场景是首选的焦点环境。 这实质上意味着您的 SKScenes 将处理首选焦点而不是 GameViewController。

在 SpriteKit 游戏中,SKScene 应使用 SpriteKit API(如 SKLabelNodes、SKSpriteNodes 等)处理 UI(例如按钮)。因此,您需要将首选焦点传递给 SKScene。

class GameViewController: UIViewController {

      override func viewDidLoad() {
           super.viewDidLoad()

           // default code to present your 1st SKScene.
      }
}

#if os(tvOS)
extension GameViewController {

      /// Tell GameViewController that the currently presented SKScene should always be the preferred focus environment
      override var preferredFocusEnvironments: [UIFocusEnvironment] {
           if let scene = (view as? SKView)?.scene {
               return [scene]
           } 
           return []
      }
 }
 #endif

您的按钮应该是 SKSpriteNode 的子类,您将用于游戏中的所有按钮。使用枚举并为它们提供不同的名称/标识符,以便在按下它们时区分它们(查看 Apples 示例游戏 DemoBots)。

 class Button: SKSpriteNode {

      var isFocusable = true // easy way to later turn off focus for your buttons e.g. when overlaying menus etc.

      /// Can become focused
      override var canBecomeFocused: Bool {
          return isFocusable
      }

      /// Did update focus
      override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {

          if context.previouslyFocusedItem === self {
              // SKAction to reset focus animation for unfocused button
          }

          if context.nextFocusedItem === self {
               // SKAction to run focus animation for focused button
          }
      }
 }

比在您的开始场景中,您可以将焦点环境设置为您的 playButton 或任何其他按钮。

class StartScene: SKScene {
   ....
}

#if os(tvOS)
extension StartScene {
      override var preferredFocusEnvironments: [UIFocusEnvironment] {
          return [playButton]
     }
}
#endif

如果您在场景中叠加菜单或其他 UI,您可以执行类似的操作,例如 GameScene(如果需要,将焦点移至 gameMenuNode)

class GameScene: SKScene {
   ....
}

#if os(tvOS)
extension GameScene {

      override var preferredFocusEnvironments: [UIFocusEnvironment] {
         if isGameMenuShowing { // add some check like this
             return [gameMenuNode]
         }
         return [] // empty means scene itself
     }    
}
#endif

当您在 SKScene 之间转换时(例如 StartScene -> GameScene),您还必须告诉 GameViewController 更新其焦点环境。如果您使用 SKTransitions,这一点尤其重要,我花了一段时间才弄清楚这一点。如果您使用 SKTransitions,而新旧场景在过渡期间处于活动状态,则 GameViewController 将使用旧场景首选焦点环境而不是新场景,这意味着新场景将无法正确聚焦。

每次在场景之间转换时,我都会这样做。您将不得不稍作延迟,否则将无法正常工作。

 ...
 view?.presentScene(newScene, transition: ...)

 #if os(tvOS)
    newScene.run(SKAction.wait(forDuration: 0.02)) { // wont work without delay
         view?.window?.rootViewController?.setNeedsFocusUpdate()
         view?.window?.rootViewController?.updateFocusIfNeeded()
    }
 #endif

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多