我一直在寻找有关如何正确执行此操作的教程,而 Google 一直空着。也许这是一个非常简单的主题,但我不知道该怎么做。
到目前为止,我发现的代码如下所示:
if (currentKeyboardState.IsKeyDown(Keys.A))
{
RotationAngle += 0.01f;
float circle = MathHelper.Pi * 2;
RotationAngle = RotationAngle % circle;
}
只是,这并没有多大作用。它来自MSDN。这是我能找到的最好看的解决方案。
我想做的就是让玩家旋转他们的船并朝另一个方向射击。我正在制作一款与小行星非常相似的游戏,但目前我只能朝一个方向射击。
任何帮助,将不胜感激 :)
编辑:这是我当前的 Player.cs:
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace GameTest
{
class Player
{
public Texture2D PlayerTexture;
public Vector2 Position;
public bool Alive;
public int Health;
public Vector2 origin;
public float RotationAngle;
KeyboardState currentKeyboardState;
KeyboardState previousKeyboardState;
public int Width
{
get { return PlayerTexture.Width; }
}
public int Height
{
get { return PlayerTexture.Height; }
}
public void Initialize(Texture2D texture, Vector2 position)
{
PlayerTexture = texture;
Position = position;
Alive = true;
Health = 10;
origin.X = PlayerTexture.Width / 2;
origin.Y = PlayerTexture.Height / 2;
}
public void Update(GameTime gameTime)
{
RotationAngle += 10f;
previousKeyboardState = currentKeyboardState;
currentKeyboardState = Keyboard.GetState();
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
if (currentKeyboardState.IsKeyDown(Keys.A))
{
//float circle = MathHelper.Pi * 2;
//RotationAngle = RotationAngle % circle;
}
if (currentKeyboardState.IsKeyDown(Keys.D))
{
//rotation
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(PlayerTexture, Position, null, Color.White, RotationAngle, origin, 1f, SpriteEffects.None, 0f);
}
}
}