【发布时间】:2013-12-20 18:30:28
【问题描述】:
我正在创建一个基于三消的游戏。游戏功能齐全,我唯一需要的就是动画。现在我想切换两个精灵的位置,所以我想如果我只是画在后面的东西上,我就不必担心切换任何东西,因为它只会在视觉上发生。
public void Switch2(GameTime gameTime)
{
Vector2 sPos1 = new Vector2(16 + 50 * firstXint, 16 + 50 * firstYint);
Vector2 sPos2 = new Vector2(16 + 50 * secondXint, 16 + 50 * secondYint);
Vector2 a2 = new Vector2(16 + 50 * firstXint, 16 + 50 * firstYint);
Vector2 b2 = new Vector2(16 + 50 * secondXint, 16 + 50 * secondYint);
Vector2 ag2 = new Vector2(14 + 50 * firstXint, 14 + 50 * firstYint);
Vector2 bg2 = new Vector2(14 + 50 * secondXint, 14 * secondYint);
if (sPos1.X <= b2.X)
{
sPos1 = sPos1 + spriteSpeedX * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
if (sPos2.X >= a2.X)
{
sPos2 = sPos2 - spriteSpeedX * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
spriteBatch.Begin();
spriteBatch.Draw(gridTexture, ag2, Color.White);
spriteBatch.End();
spriteBatch.Begin();
spriteBatch.Draw(gridTexture, bg2, Color.White);
spriteBatch.End();
spriteBatch.Begin();
spriteBatch.Draw(apple, sPos1, Color.WhiteSmoke);
spriteBatch.End();
spriteBatch.Begin();
spriteBatch.Draw(strawberry, sPos2, Color.WhiteSmoke);
spriteBatch.End();
}
如果我将Vector2 放在此方法上方,这将起作用,但随后Vector2 无法使用firstXint(玩家点击的地方)等。然后如果我再尝试这种方式精灵不会移动,因为游戏总是从一开始就调用该方法,因此sPos1 和sPos2 永远不会改变。顺便说一下,然后在Draw()方法中调用该方法。
关于如何解决此问题的任何想法?我什么都想不出来。 (可能是因为我已经使用 XNA 2 天了,之前游戏是在 windows 窗体应用程序中,而不是精灵只有数字)
【问题讨论】:
-
我猜你有某种
Tile类。向此类添加TargetPosition属性。当您切换两个磁贴时,更新TargetPosition。在 XNA 的Update方法中,如果实际位置与目标不同,则更新实际位置。在 XNA 的Draw方法中使用实际位置来绘制图块。