我目前正在XNA制作游戏。到目前为止我一直很顺利,但我不知道如何在运行游戏时改变精灵的纹理。这方面的一个例子是。当角色仍然是一个图像时,那么当他走路时,这是一个不同的图像。我怎么能做到这一点?
答案 0 :(得分:0)
运行检查,只需将实例的Texture2D texture
设置为预加载库中的其他纹理。
我通常将内容文件夹中的所有纹理加载到字典中并使用如下:
var StringTextureDic = new Dictionary<string, Texture2D>();
// code that loads all textures into the dictionary, file names being keys
// whenever I need to assign some texture, I do this:
if (!playerIsMoving)
Player.texture = StringTextureDic["player standing"];
if (playerIsMoving)
Player.texture = StringTextureDic["player moving"];
答案 1 :(得分:0)
Rectangle frame;
int curFrame;
int frameWidth;
int frameHeight;
int runAnimationLength;
Update()
{
//Handle your "animation code"
if(playerIsMoving)
curFrame++; //Running
if(curFrame == runAnimationLength)
curFrame =0;
else
curFrame = 0; //Standing still
}
Draw(SpriteBatch spriteBatch)
{
frame = new Rectangle(curFrame*frameWidth,curFrame*frameHeight,frameWidth,frameHeight);
spriteBatch.Draw(
texture,
position,
**frame**,
color,
rotation,
origin,
SpriteEffects.None,
1);
}