【发布时间】: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