简体   繁体   English

Windows Phone 8加速计事件

[英]Windows Phone 8 Accelerometer events

I'm making my first game for Windows Phone (XNA). 我正在为Windows Phone(XNA)制作我的第一款游戏。 I use Accelerometer to change the position of a crosshair on the screen: 我使用Accelerometer来改变屏幕上十字准线的位置:

十字准线的位置

Here is the code in my Initialize() function (note that Accelerometer is local variable declared only in this function): 这是我的Initialize()函数中的代码(请注意,Accelerometer是仅在此函数中声明的局部变量):

Accelerometer accelerometer = new Accelerometer();
accelerometer.CurrentValueChanged += accelerometer_CurrentValueChanged;
accelerometer.Start();

And the event handler: 和事件处理程序:

void accelerometer_CurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
        {
            lock (accelerometerVectorLock)
            {
                accelerometerVector = new Vector3(
                    (float)e.SensorReading.Acceleration.X,
                    (float)e.SensorReading.Acceleration.Y,
                    (float)e.SensorReading.Acceleration.Z);
            }
        }

This works fine on Windows Phone Emulator, and on my Nokia Lumia 520 connected to the computer and launching from Visual Studio, however when I launch the game in the phone (not connected to the computer), the accelerometer_CurrentValueChanged event appears to be called only once, on application startup. 这适用于Windows Phone模拟器,以及连接到计算机并从Visual Studio启动的我的诺基亚Lumia 520,但是当我在手机中启动游戏(未连接到计算机)时,加速计_CurrentValueChanged事件似乎只被调用一次,在应用程序启动时。

My solution was to make the accelerometer a member of my Game class, then code in Initialize() like this: 我的解决方案是让加速度计成为我的Game类的成员,然后在Initialize()中编码,如下所示:

accelerometer = new Accelerometer();
accelerometer.CurrentValueChanged += accelerometer_CurrentValueChanged;
accelerometer.Start();

So my question is, why does this solution work? 所以我的问题是, 为什么这个解决方案有效? And why is there a difference between application launched from VS and normally, even on the same device? 为什么从VS启动的应用程序和通常在同一设备上的应用程序之间存在差异?

Why this solution works? 为何此解决方案有效?

This solution works because you're keeping a reference to the accelerometer. 此解决方案有效,因为您保留对加速度计的引用。 Windows Phone applications, like all .NET applications, use an automated system for memory management. 与所有.NET应用程序一样,Windows Phone应用程序使用自动化系统进行内存管理。 A background process, called garbage collector, inspects the objects in a regular basis, detects those who are not referenced anymore, and clean them. 后台进程称为垃圾收集器,定期检查对象,检测不再引用的对象,并清除它们。 If you declare accelerometer as a local variable, it won't be referenced anymore when the function exits, and will therefore be cleaned. 如果将Accelerometer声明为局部变量,则在函数退出时不再引用它,因此将被清除。 When you declare it as a member of your class, it will be alive for as long as your class lives. 当你宣布它是你班级的一员时,只要你的班级存在,它就会活着。

Why the difference between application launched from VS and normally, on the same device? 为什么从VS启动应用程序与通常在同一设备上的区别?

When launching the code from Visual Studio, a debugger is attached. 从Visual Studio启动代码时,会附加调试器。 To help you debugging, it has some impacts on the way the code executes. 为了帮助您进行调试,它会对代码的执行方式产生一些影响。 Notably, it makes the garbage collector way less aggressive. 值得注意的是,它使垃圾收集器的方式更具攻击性。 It explains why you didn't have this issue when testing with the debugger attached. 它解释了为什么在使用附加的调试器进行测试时没有遇到此问题。 Note that you can achieve the same result by pressing Control + F5 in Visual Studio: it'll start the application without attaching the debugger. 请注意,通过在Visual Studio中按Control + F5可以获得相同的结果:它将在不附加调试器的情况下启动应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM