我有一个问题就是知道如何以tvOS方式制作菜单按钮。我知道UIKit按钮如何聚焦的新方式,但它们之间的导航是在幕后完成的。在SpriteKit中没有FocusEngine支持,所以这里有我的问题,请你们给一个提示或代码片段(最好是swift)如何创建一个SKSpriteNode按钮以及如何实现它以便它可以像一个按钮一样工作?
谢谢
答案 0 :(得分:0)
我不确定这是否是最好的方法,但它适用于我和我的tvOS游戏。我使用了手势识别器,我使用左右滑动来改变我的SKSpriteNode纹理。这会产生选定/取消选择按钮的效果。比我简单地使用变量buttonSelected这是bool类型,每当按钮纹理改变时我都会更新它。我使用此变量来决定在单击遥控器后应执行的操作。这是通过buttonAction函数中的简单if语句完成的。我希望这能帮到您。我仍然很新,所以我希望我能清楚地描述它。
class GameScene: SKScene {
var button = SKSpriteNode(imageNamed: "button.png")
var buttonSelected = false
var button2 = SKSpriteNode(imageNamed: "button2.png")
var button2Selected = false
override func didMoveToView(view: SKView) {
button.position = CGPoint(x: 900, y:100)
self.addChild(button)
button2.position = CGPoint(x: 1700, y:100)
self.addChild(button2)
// gesture recognizer
let swipeRight:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(GameOverScene.swipedRight(_:)))
swipeRight.direction = .Right
view.addGestureRecognizer(swipeRight)
let swipeLeft:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(GameOverScene.swipedLeft(_:)))
swipeLeft.direction = .Left
view.addGestureRecognizer(swipeLeft)
let tapGeneralSelection = UITapGestureRecognizer()
tapGeneralSelection.addTarget(self, action: #selector(GameScene.buttonAction))
tapGeneralSelection.allowedPressTypes = [NSNumber (integer: UIPressType.Select.rawValue)]
self.view!.addGestureRecognizer(tapGeneralSelection)
}
func swipedRight(sender:UISwipeGestureRecognizer){
let buttonTexture1 = SKTexture(imageNamed: "button2Selected.png")
button2.runAction(SKAction.setTexture(buttonTexture1))
button2Selected = true
let buttonTexture2 = SKTexture(imageNamed: "button.png")
button.runAction(SKAction.setTexture(buttonTexture2))
buttonSelected = false
}
func swipedLeft(sender:UISwipeGestureRecognizer){
let buttonTexture1 = SKTexture(imageNamed: "buttonSelected.png")
button.runAction(SKAction.setTexture(buttonTexture1))
buttonSelected = true
let buttonTexture2 = SKTexture(imageNamed: "button2.png")
button2.runAction(SKAction.setTexture(buttonTexture2))
button2Selected = false
}
func buttonAction() {
if( buttonSelected == true) {
// do something after button is pressed
} else if (button2Selected == true) {
// do something after button2 is pressed
}
}
}