【发布时间】:2013-12-24 22:29:08
【问题描述】:
我想在 Windows Phone 游戏中使用加速度计移动球。但是当我倾斜 Windows Phone 设备时,球的移动不正确。
例如,如果我将设备向左倾斜,球不会向左移动,而是会非常奇怪地移动。我不知道我可以在我的代码中更改什么以使球正确移动。
如果我倾斜 Windows Phone 设备,我应该如何更改才能使球正确移动?
更新: 我更改了代码,但球没有朝正确的方向移动。 你可以在这里下载我的项目:http://www.file-upload.net/download-8439759/WindowsPhoneGame22.rar.html
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Motion motion;
Texture2D Ball;
Vector2 BallPos = new Vector2(400, 300);
Vector3 accelReading = new Vector3();
Vector3 speed = new Vector3();
void motion_CurrentValueChanged(object sender, SensorReadingEventArgs<MotionReading> e)
{
UpdateUI(e.SensorReading);
}
private void UpdateUI(MotionReading e)
{
accelReading.X = e.DeviceAcceleration.X;
accelReading.Y = e.DeviceAcceleration.Y;
accelReading.Z = e.DeviceAcceleration.Z;
accelReading.Normalize();
Vector3 currentAccelerometerState = accelReading;
if (currentAccelerometerState.X != 0)
speed += new Vector3(currentAccelerometerState.X, 0, 0);
if (currentAccelerometerState.Z != 0)
speed += new Vector3(0, 0, -currentAccelerometerState.Z);
if (speed.Length() > 2)
{
speed.Normalize();
speed *= 2;
}
BallPos.X += speed.X;
BallPos.Y += speed.Y;
}
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
TargetElapsedTime = TimeSpan.FromTicks(333333);
InactiveSleepTime = TimeSpan.FromSeconds(1);
graphics.IsFullScreen = true;
}
protected override void Initialize()
{
if (Motion.IsSupported)
{
motion = new Motion();
motion.TimeBetweenUpdates = TimeSpan.FromMilliseconds(20);
motion.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<MotionReading>>(motion_CurrentValueChanged);
motion.Start();
}
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
Ball = Content.Load<Texture2D>("ballbig");
}
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(Ball, BallPos, null, Color.White, rotation, new Vector2(Ball.Width/2,Ball.Height/2), 1f, SpriteEffects.None,1);
spriteBatch.End();
base.Draw(gameTime);
}
}
【问题讨论】:
标签: c# windows-phone-7 windows-phone-8 xna