0

我目前正在 XNA 中制作游戏。到目前为止我进展顺利,但我不知道如何在运行游戏时更改精灵的纹理。这就是一个例子。当角色静止时,这是一个形象,然后当他走路时,这是一个不同的形象。我将如何做到这一点?

4

2 回答 2

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"];
于 2013-01-20T05:33:42.103 回答
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);
}
于 2013-01-20T10:43:54.617 回答