我试图用新闻开始定义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()
}
答案 0 :(得分: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)
}
如果您正在寻找完全成熟的微型游戏手柄支持,那么您将需要观看有关gameController框架的一些教程。
4)尝试将类似于UserDefaults的字符串键放在某些属性中。
enum Key: String {
case onoroff
}
而不是像这样使用
UserDefaults.standard.set(true, forKey: Key.onoroff.rawValue)
避免拼写错误。
5)你应该始终遵循Swift惯例,你的一些方法和属性以大写字母开头,但它们不应该。
我建议您重新构建代码,而不是继续尝试使用GameViewController来实现这一切。应该直接在相关的SKScene中完成。
EDIT。我认为你正在调用选择器错误,试试这个。当你的函数有一个参数你会使用这个(_ :),你试图使用(手势:)。试试这个。
... action: #selector(startGameRecognizer(_:))
希望这有帮助