英文:
How to continuously test for the connection of a Game Controller?
问题
如何持续测试游戏手柄的连接?
暂时离开代码...
我想做什么?
-
在应用程序初始启动时,我想等待"gamepadconnected"事件。
-
一旦我通过打开游戏手柄得到了这个事件,我立即改变一个SKSpriteNode的颜色,然后开始寻找:
a) 游戏手柄上的按钮按下和
b) "gamepaddisconnected"事件
- 因此,当我基于2(a)执行代码时,当我关闭游戏手柄时,我收到2(b)。我立即改变上述颜色为不同的颜色,然后寻找另一个"gamepadconnected"事件。
来回切换。
来回切换。
我尝试使用苹果自家的NotificationCenter代码,它可以正常工作,但有一个重大遗漏,即他们的代码从不触发"gamepaddisconnected"事件,这是由我放置的多个"print"语句确定的。
== 我到目前为止所做的 ==
我现在在我的GameViewController的viewDidLoad()中调用苹果自家的ObserveForGameControllers()。
我的Nimbus+游戏手柄被设置为关闭。
我最初运行我的项目,苹果的代码一直显示"CONNECTED"。
此外,苹果表示他们的两个NotificationCenter.default.addObserver()的@objc函数会被连续调用。
以下是苹果的代码:
func ObserveForGameControllers() {
NotificationCenter.default.addObserver(
self,
selector: #selector(connectControllers),
name: NSNotification.Name.GCControllerDidConnect,
object: nil) // means <Any> Object can send Message
NotificationCenter.default.addObserver(
self,
selector: #selector(disconnectControllers),
name: NSNotification.Name.GCControllerDidDisconnect,
object: nil)
} // ObserveForGameControllers
苹果的connectControllers
选择器通过等待用户按下按钮来启动动作,然后再执行其他操作。
如果按下按钮,则执行代码,例如,将游戏棋子移动到特定方向。
现在,如果由于任何原因,在游戏运行时我断开游戏手柄的连接,例如,通过按下它的Home
按钮,我想调用我的stopGame()
执行某些操作,比如上面指定的改变颜色。这次对stopGame()
的调用被放置在我的disconnectControllers
选择器中。
但是,它从不被调用!
我应该能够在游戏进行时切换我的Nimbus+的连接...
由于苹果声明这两个addObserver()
的@objc函数会被连续调用,我故意在每个@objc中加入了print语句。
在这种切换过程中,应该有一大堆打印语句在“connected”和“dis-connected”之间切换。
但是从来没有发生!
== 我到目前为止所做的结束 ==
显然,我可以使用替代苹果的NotificationCenter代码。
然而,我真的更愿意坚持使用后者。
我只是希望我知道如何正确地实现它。
注意:有些人指责Xcode模拟器,因为它显然不是一个真实的设备。
嗯,我已经将我的应用安装在我的Apple TV上,但是同样的挑战依然存在。
英文:
How to continuously test for the connection of a Game Controller?
Stepping back from code just for a moment ...
What do I want to do?
1) upon initial start up of app, I want to wait for a gamepadconnected Event.
2) Once I get this Event by turning on my Game Controller, I immediately change a SKSpriteNode's color and then start looking for:
a) button presses on the gamepad and
b) a gamepaddisconnected Event
3) So while I am executing code based on 2(a), I receive 2(b) when I turn off my Game Controller. I immediately change the above color to something different, followed by looking for another gamepadconnected Event.
Back-and-forth.
Back-and-forth.
I have tried using Apple's very own NotificationCenter
Code which works except for a significant omission, namely, their Code never triggers a gamepaddisconnected Event as determined by my placement of multiple print
statements.
== start what I have done so far ==
I now call Apple's very own ObserveForGameControllers() within my GameViewController's viewDidLoad().
My Nimbus+ Game Controller is set to off.
I initially Run my Project, Apple's Code consistently shows "CONNECTED".
Furthermore, Apple states that their two NotificationCenter.default.addObserver() @objc funcs are continuously called.
Here's Apple's code:
func ObserveForGameControllers() {
NotificationCenter.default.addObserver(
self,
selector: #selector(connectControllers),
name: NSNotification.Name.GCControllerDidConnect,
object: nil) // means <Any> Object can send Message
NotificationCenter.default.addObserver(
self,
selector: #selector(disconnectControllers),
name: NSNotification.Name.GCControllerDidDisconnect,
object: nil)
} // ObserveForGameControllers
Apple's connectControllers
selector starts the action by waiting for the user to press a Button before doing anything else.
If a Button is pressed then code is executed, e.g., moving a Game Piece in a specific direction.
Now, if for any reason, I disconnect the Game Controller while the Game is running, e.g., via pressing its Home
Button, I want to call my stopGame()
to do certain things, like changing colors as specified above. This call to stopGame()
is placed in my disconnectControllers
selector.
But, it's never called!
I should be able to toggle my Nimbus+ off and on while playing ...
Since Apple states that these two addObserver() @objc funcs are cotinuously called. I deliberately have print statements in each @objc.
With this toggling is going on, there should be a whole slew of print statements that toggle between "connected" and "dis-connected".
Never happens!!
== end what I have done so far ==
Clearly, I am open to use alternatives to Apple's NotificationCenter code.
Nevertheless, I really would prefer to stick with the latter.
I just wish I knew how to correctly implement it.
Note: some have blamed the Xcode Simulator because it is obviously not a real device.
Well, I have installed my App on my Apple TV and the same challenges persist.
答案1
得分: 1
问题已解决--
首先,我要赞扬Keith在Apple DTS的贡献,没有他,我永远无法找出问题的原因。他在这个问题上的帮助完全抵得上我支付的99美元。
解决方案的一个重要部分在于刚刚发布的Xcode 14.3的更新以及tvOS 16.4的更新。
现在,预期的CONNECTED和DIS-CONNECTED控制台消息如预期般出现。
我还有一些工作要做,但至少Apple的
NotificationCenter.default.addObserver(..)
按照Apple的广告宣传的那样工作了。
哇哦!!
英文:
SOLVED --
1st and foremost, I want to sing the praises of Keith at Apple DTS without whom I could never have chased down the cause of my problem. His assistance on this one issue totally pays for my $99.
A significant part of the solution rests with the just released update of Xcode to 14.3 and tvOS to 16.4
Now the expected CONNECTED and DIS-CONNECTED Console messages appear as anticipated.
I've got some more work to do, but at least Apple's
NotificationCenter.default.addObserver(..)
works as Apple advertises.
WHOOPIE!!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论