##### Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

# Windows Phone 7 摇动检测

SDK: Windows Phone 7.1 SDK

WS - OtomiiLu 在 09 Aug 2012 创建

## 摇动支持

`public enum ActionEnum    {        None = 0,        LeftRightShake = 1,        UpDownShake = 2}`

`accHelper.SupportedAction = ActionEnum.LeftRightShake | ActionEnum.UpDownShake;`

`(SupportedAction & ActionEnum.LeftRightShake).Equals(ActionEnum.LeftRightShake)`

LeftRightShake = 0001,

UpDownShake = 0010

ActionEnum.LeftRightShake | ActionEnum.UpDownShake = 0011

SupportedAction & ActionEnum.LeftRightShake = 0001,去掉了属于UpDownShake的那个1

`(SupportedAction & ActionEnum.LeftRightShake).Equals(ActionEnum.LeftRightShake)`

## 重力坐标类Axis

`    public class Axis    {        public double X { get; set; }        public double Y { get; set; }        public double Z { get; set; }        public DateTime CurrentDateTime { get; set; }        public AccelerationDirection AccDirection { get; set; }    }`

`public enum ActionEnum    {        None = 0,        LeftRightShake = 1,        UpDownShake = 2    }`

## 重力坐标集合类AxisCollection

AxisCollection是最主要的一个类,提供了很多的方法,为了重力判断的使用.

`        public void Add(Axis axis)        {            var Gravity = axis.X * axis.X + axis.Y * axis.Y + axis.Z * axis.Z;            if (Gravity < 0.8 || Gravity > 1.2)            {                AxisList.Add(axis);                if (-0.3 > axis.X || axis.X > 0.3)                {                    if (axis.X < -0.3) axis.AccDirection = AccelerationDirection.Left;                    else if (axis.X > 0.3) axis.AccDirection = AccelerationDirection.Right;                }                else                {                    if ((axis.Y * axis.Y + axis.Z * axis.Z > 1.2)) axis.AccDirection = AccelerationDirection.Up;                    else if (axis.Y * axis.Y + axis.Z * axis.Z < 0.8) axis.AccDirection = AccelerationDirection.Down;                }                axis.CurrentDateTime = DateTime.Now;            }        }`

`        public bool IsMoving()        {            if (AxisList.Count != 0)            {                if (DateTime.Now.Subtract(AxisList.Last().CurrentDateTime).TotalMilliseconds > 666)                {                    return false;                }            }            return true;        }`

`        public void CheckCurrentAction()        {            if (AxisList.Count == 0) CurrentActionEnum = ActionEnum.None;            else            {                if (!(CurrentActionEnum .Equals( ActionEnum.None)))                {                    return;                }                if ((SupportedAction & ActionEnum.LeftRightShake).Equals(ActionEnum.LeftRightShake) &&                    AxisList.Where(al => al.AccDirection.Equals(AccelerationDirection.Left)).Count() > 2 && AxisList.Where(al => al.AccDirection.Equals(AccelerationDirection.Right)).Count() > 2)                    CurrentActionEnum = ActionEnum.LeftRightShake;                else if ((SupportedAction & ActionEnum.UpDownShake).Equals(ActionEnum.UpDownShake) &&                    AxisList.Where(al => al.AccDirection.Equals(AccelerationDirection.Up)).Count() > 2 && AxisList.Where(al => al.AccDirection.Equals(AccelerationDirection.Down)).Count() > 2)                    CurrentActionEnum = ActionEnum.UpDownShake;            }        }`

`        public bool Reset()        {            try            {                AxisList.Clear();            }            catch (Exception)            {                return false;            }            return true;        }`

## 帮助类AccHelper

AccHelper类的主要功能就是帮助我们进行中心控制,封装一些我们不需要处理的操作. 下面,就是整个WPAcchelper类

`namespace AccHelper{    public class WPAccHelper    {        private Accelerometer accelerometer = new Accelerometer();        private AxisCollection axisCollection = new AxisCollection();        public ActionEnum SupportedAction        {            get { return axisCollection.SupportedAction; }            set { axisCollection.SupportedAction = value; }        }        public WPAccHelper()        {            accelerometer.Start();            accelerometer.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<AccelerometerReading>>(accelerometer_CurrentValueChanged);        }         void accelerometer_CurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)        {            var newAxis = new Axis() { X = e.SensorReading.Acceleration.X, Y = e.SensorReading.Acceleration.Y, Z = e.SensorReading.Acceleration.Z };            axisCollection.Add(newAxis);            if (!axisCollection.IsMoving())            {                axisCollection.Reset();                return;            }            axisCollection.CheckCurrentAction();        }         public string GetInfo()        {            return axisCollection.CurrentActionEnum.ToString();        }    }}`

## GamePage页面

• 声明一个WPAcchelper类,并设定支持的晃动类型是什么
`WPAccHelper accHelper;`
`accHelper = new WPAccHelper();            accHelper.SupportedAction = ActionEnum.LeftRightShake | ActionEnum.UpDownShake;`

`        private void OnDraw(object sender, GameTimerEventArgs e)        {            SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.Black);            spriteBatch.Begin();            spriteBatch.DrawString(MySprite, accHelper.GetInfo(), new Vector2(0, 0), Color.White);            spriteBatch.End();            // TODO: Add your drawing code here        }`

## 项目文件和总结

```本项目指在提供一种思想，如何通过重力，检测当前的行为，重力的检测，在游戏中经常的用到，是非常重要的一部分.
```