我正在尝试使用类构建一个 Asteroid 类型的 remake,我在这里有一个用于制作船的类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace AsteroidsTest {
class Character {
KeyboardState inputKeyboard;
Texture2D texture;
Rectangle rectangle;
Vector2 position;
Vector2 origin;
Vector2 speed;
const float tangentialVelocity = 5f;
public float rotation;
public Character(Texture2D newTexture, int positionX, int positionY) {
texture = newTexture;
position = new Vector2(positionX, positionY);
}
public void Update(GameTime gametime, Keys KeyUp, Keys KeyRight, Keys KeyLeft) {
inputKeyboard = Keyboard.GetState();
rectangle = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
position += speed;
origin = new Vector2(rectangle.Width / 2, rectangle.Height / 2);
if (inputKeyboard.IsKeyDown(KeyRight)) {
rotation += 0.1f;
}
else if (inputKeyboard.IsKeyDown(KeyLeft)) {
rotation -= 0.1f;
}
if (inputKeyboard.IsKeyDown(KeyUp)) {
speed.X = (float)Math.Cos(rotation) * tangentialVelocity;
speed.Y = (float)Math.Sin(rotation) * tangentialVelocity;
} else if (speed != Vector2.Zero) {
speed = Vector2.Zero;
}
}
public void Draw(SpriteBatch spriteBatch) {
spriteBatch.Draw(texture, position, null, Color.White, rotation, origin, 1.0f, SpriteEffects.None, 0);
}
}
}
但是当它使用我的 Main 类在精灵上的纹理下方绘制时,它是正常绘制的,但是当我按下 w 向前移动时,它会将我移动到右侧。当我旋转它时,就好像我的船的顶部在一边,这怎么可能?
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace AsteroidsTest {
public class Asteroids : Microsoft.Xna.Framework.Game {
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Character Ship;
Texture2D playerTexture;
public Asteroids() {
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize() {
base.Initialize();
}
protected override void LoadContent() {
spriteBatch = new SpriteBatch(GraphicsDevice);
playerTexture = Content.Load<Texture2D>("Actors//Ship");
Ship = new Character(playerTexture, 16, 16);
}
protected override void UnloadContent() {
}
protected override void Update(GameTime gameTime) {
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
this.Exit();
Ship.Update(gameTime, Keys.Up, Keys.Right, Keys.Left);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime) {
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
Ship.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
编辑:似乎只需将图像顺时针旋转 90 度即可修复它,但不幸的是它默认为该角度。