我正在尝试使用类构建一个Asteroid类型的重拍,我有一个类来制造这里的船:
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度顺时针修复它,但遗憾的是它默认为该角度。
答案 0 :(得分:0)
我认为它按预期工作 - 默认情况下(0旋转)意味着对象向右看而不是向上看(相对于你的监视器/屏幕)。