【问题标题】:XNA animation switching position of two sprites两个精灵的XNA动画切换位置
【发布时间】: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(玩家点击的地方)等。然后如果我再尝试这种方式精灵不会移动,因为游戏总是从一开始就调用该方法,因此sPos1sPos2 永远不会改变。顺便说一下,然后在Draw()方法中调用该方法。

关于如何解决此问题的任何想法?我什么都想不出来。 (可能是因为我已经使用 XNA 2 天了,之前游戏是在 windows 窗体应用程序中,而不是精灵只有数字)

【问题讨论】:

  • 我猜你有某种Tile 类。向此类添加 TargetPosition 属性。当您切换两个磁贴时,更新TargetPosition。在 XNA 的Update 方法中,如果实际位置与目标不同,则更新实际位置。在 XNA 的Draw 方法中使用实际位置来绘制图块。

标签: c# arrays xna 2d sprite


【解决方案1】:

首先,当你执行几个spriteBatch.Draw时,你不必每次都调用BeginEnd,这样效率很低,你只需要这样做:

spriteBatch.Begin();
spriteBatch.Draw(gridTexture, ag2, Color.White);
spriteBatch.Draw(gridTexture, bg2, Color.White);
spriteBatch.Draw(apple, sPos1, Color.WhiteSmoke);
spriteBatch.Draw(strawberry, sPos2, Color.WhiteSmoke);
spriteBatch.End();

对于您的主要问题,您写道您在Draw 中调用此方法,因此每个游戏周期都会调用此方法,主要问题是您初始化sPos1sPos2 并使用@ 更新它们的值987654329@.

gameTime.ElapsedGameTime 是固定的,会计算每个周期之间的时间,并且始终约为 16 毫秒(如果您的游戏每秒更新 60 次)。这意味着您的精灵的位置永远不会改变。
你必须使用gameTime.TotalGameTime,它会记录游戏开始以来的时间,这应该会移动你的精灵。

参考MSDN

【讨论】:

    猜你喜欢
    • 2014-01-27
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多