【发布时间】:2016-07-04 10:42:57
【问题描述】:
我在我的游戏中缩小了它,所以它在游戏中并没有那么大。
所以无论如何我想要的下面代码的结果:
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
spritePos = spriteVelocity + spritePos;
spriteR = new Rectangle((int)spritePos.X, (int)spritePos.Y, spriteT.Width, spriteT.Height);
spriteOrigin = new Vector2(spriteR.Width/2, spriteR.Height / 2);
if (Keyboard.GetState().IsKeyDown(Keys.Right)) rotation += 0.1f;
if (Keyboard.GetState().IsKeyDown(Keys.Left)) rotation -= 0.1f;
if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
spriteVelocity.X = (float)Math.Cos(rotation) * tangentialVelocity;
spriteVelocity.Y = (float)Math.Sin(rotation) * tangentialVelocity;
} else if (spriteVelocity != Vector2.Zero)
{
float i = spriteVelocity.X;
float j = spriteVelocity.Y;
spriteVelocity.X = i -= friction * i;
spriteVelocity.Y = j -= friction * j;
}
base.Update(gameTime);
}
然后是Draw方法:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
foreach(Bushes bush in bushes)
{
bush.Draw(spriteBatch);
}
player.Draw(spriteBatch);
spriteBatch.Draw(spriteT, spritePos, null, Color.White, rotation, spriteOrigin, 0.1f, SpriteEffects.None, 0f);
spriteBatch.End();
base.Draw(gameTime);
}
当我按下“向上”箭头时,这个小火箭应该朝着它所指向的方向前进(因为我可以用左右箭头旋转它以使其指向所需的位置),但是这段代码的结果是当我按下向上箭头时,精灵首先向右移动,当它旋转时它不会去它指向的地方而是有点侧向:/
我在这里做错了什么?
PS。所有未在代码中声明和初始化的变量都是全局变量,并在 Initialize() 和 LoadContent() 方法中初始化:/
【问题讨论】: