我正在制作一个简单的游戏来实现加速度计数据,这个想法是加速度计数据决定了在屏幕上漫无目的地想知道球的加速度,球不会从屏幕的边缘掉下来,所以当它撞到墙壁时,速度会反转。
问题是有时候球的行为非常好并且正如预期的那样,有时候,它会在屏幕上跳起来,我无法追踪问题,这是代码:
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D BallTexture;
Vector2 BallPosition;
Vector2 BallSpeed;
Vector2 BallAcc;
float CoFriction = 0.15F;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
//set to full screen
graphics.IsFullScreen = true;
// Frame rate is 30 fps by default for Windows Phone.
TargetElapsedTime = TimeSpan.FromTicks(333333);
// Extend battery life under lock.
InactiveSleepTime = TimeSpan.FromSeconds(1);
}
protected override void Initialize()
{
//speed,position,acceleration
BallSpeed = Vector2.Zero;
BallAcc = Vector2.Zero;
BallPosition = new Vector2(240,400);
//Accelerometer
Accelerometer acc = new Accelerometer();
acc.ReadingChanged += new EventHandler<AccelerometerReadingEventArgs>(readingChanged);
acc.Start();
base.Initialize();
}
public void readingChanged(object sender, AccelerometerReadingEventArgs e)
{
Deployment.Current.Dispatcher.BeginInvoke(() => handleAccChange(e));
}
public void handleAccChange(AccelerometerReadingEventArgs e)
{
BallAcc.X = -(float)Math.Round((float)e.Y);
BallAcc.Y = -(float)Math.Round((float)e.X);
}
protected override void LoadContent()
{
BallTexture = Content.Load<Texture2D>("football2");
spriteBatch = new SpriteBatch(GraphicsDevice);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
BallSpeed += BallAcc;
BallPosition += BallSpeed;
//Check Borders
if (BallPosition.X < 0 || BallPosition.X > 800 - 51)
{
if (BallPosition.X < 0)
BallPosition.X = 5;
else if (BallPosition.X > 800 - 51)
BallPosition.X = 800 - 55;
BallSpeed.X = -BallSpeed.X * CoFriction;
if (BallPosition.Y < 0 || BallPosition.Y > 480 - 51)
{
if (BallPosition.Y < 0)
BallPosition.Y = 5;
if (BallPosition.Y > 480 - 51)
BallPosition.Y = 480 - 55;
BallSpeed.Y = -BallSpeed.Y * CoFriction;
}
base.Update(gameTime);
return;
}
if (BallPosition.Y < 0 || BallPosition.Y > 480 - 51)
{
BallSpeed.Y = -BallSpeed.Y*CoFriction;
if (BallPosition.Y < 0)
BallPosition.Y = 5;
if (BallPosition.Y > 480 - 51)
BallPosition.Y = 480 - 55;
if (BallPosition.X < 0 || BallPosition.X > 800 - 51)
{
BallSpeed.X = -BallSpeed.X*CoFriction;
if (BallPosition.X < 0)
BallPosition.X = 5;
if (BallPosition.X > 800 - 51)
BallPosition.X = 800 - 55;
}
base.Update(gameTime);
return;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(BallTexture, BallPosition, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
答案 0 :(得分:0)
嗯,我是WP7开发中的菜鸟,但VS10表示不推荐使用Accelerometer.ReadingChanged,应该使用.CurrentValueChanged事件。其余的看起来几乎一样。我发现那篇文章: http://www.silverlightshow.net/items/XNA-for-Silverlight-developers-Part-6-Input-accelerometer.aspx
向下滚动到'关于“芒果”的一些重要说明'
祝你好运,快乐编码