我最近进入了xna框架,我遇到了一个问题。 这是我的代码而且没有用
// Constructor
public Player()
{
texture = null;
position = new Vector2(350, 900);
moveSpeed = 10;
textureTitle = "playerShip";
}
// Load
public void LoadContent(ContentManager Content)
{
texture = Content.Load<Texture2D>(textureTitle);
}
// Update
public void Update(GameTime gameTime)
{
KeyboardState curKeyboardState = Keyboard.GetState();
if (curKeyboardState.IsKeyDown(Keys.W))
{
position.Y = position.Y - moveSpeed;
textureTitle = "playerShip2";
}
if (curKeyboardState.IsKeyDown(Keys.S))
position.Y = position.Y + moveSpeed;
if (curKeyboardState.IsKeyDown(Keys.A))
position.X = position.X - moveSpeed;
if (curKeyboardState.IsKeyDown(Keys.D))
position.X = position.X + moveSpeed;
}
// Draw
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, position, Color.White);
}
玩家总是被描绘成“玩家船只” (对不起我的英文)
答案 0 :(得分:2)
仅加载第一个纹理,如果您要更改纹理名称,则应再次调用Content.Load
。
然而,处理器更难以继续重新加载图像,如果图像全部加载,则效果更好。因此,不应该调用LoadContent()
部分,而应该创建第二个Texture2D,并直接更改纹理,而不是更改目录名称。
这样的事情:
//add this to your public variables
public Texture2D currentTexture = null;
// Load
public void LoadContent(ContentManager Content)
{
texture = Content.Load<Texture2D>("playerShip");
texture2 = Content.Load<Texture2D>("playerShip2");
currentTexture = texture;
}
// Update
public void Update(GameTime gameTime)
{
KeyboardState curKeyboardState = Keyboard.GetState();
if (curKeyboardState.IsKeyDown(Keys.W))
{
currentTexture = texture2;
}
//...
}
// Draw
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(currentTexture, position, Color.White);
}