【问题标题】:Animate opacity over time in XNA在 XNA 中随着时间的推移动画不透明度
【发布时间】:2011-04-27 18:47:59
【问题描述】:

我想对包含关卡名称的文本字符串的不透明度值进行动画处理,中间有延迟。

所以事件的顺序是这样的:

  1. 开始透明
  2. 在一秒钟的游戏时间内淡入纯白色
  3. 等一下
  4. 在一秒钟内再次淡出透明。

我编写的用于为 alpha 值设置动画的代码不起作用。另外,它非常难看,我相信使用 XNA 框架有更好的方法。

我在其他地方找不到任何关于这样做的建议。当然,像这样的动画值并不少见。我该怎么做?

这是我当前请求的代码(是的,这太可怕了)。

private int fadeStringDirection = +1;
private int fadeStringDuration = 1000;
private float stringAlpha = 0;
private int stringRef = 0;
private int stringPhase = 1;

...

if (!pause)
{
    totalMillisecondsElapsed += gameTime.ElapsedGameTime.Milliseconds;
    if (fadestringDirection != 0)
    {
        stringAlpha = ((float)(totalMillisecondsElapsed - stringRef) / (float)(fadeStringDuration*stringPhase)) * fadeStringDirection;
        stringAlpha = MathHelper.Clamp(stringAlpha, 0, 1);
        if (topAlpha / 2 + 0.5 == fadeStringDirection)
        {
            fadeStringDirection = 0;
            stringRef = totalMillisecondsElapsed;
            stringPhase++;
        }
    }
    else
    {
        stringRef += gameTime.ElapsedGameTime.Milliseconds;
        if (stringRef >= fadeStringDuration * stringPhase)
        {
            stringPhase++;
            fadeStringDirection = -1;
            stringRef = totalMillisecondsElapsed;
        }
    }
}

【问题讨论】:

  • 发布您的渲染代码的简化版本会很有帮助。
  • 这还不错。 ;)
  • +1 @Jeff Mercado。只需将其隐藏在 AbstractInterpolator/AbstractBouncer/whatever... 类中,并在实际使用时发现它很整洁!

标签: c# animation xna


【解决方案1】:

这是我现在的解决方案。比我以前拥有的要好得多(并且在它自己的类中)。

/// <summary>
/// Animation helper class.
/// </summary>
public class Animation
{
    List<Keyframe> keyframes = new List<Keyframe>();

    int timeline;

    int lastFrame = 0;

    bool run = false;

    int currentIndex;

    /// <summary>
    /// Construct new animation helper.
    /// </summary>
    public Animation()
    {
    }

    public void AddKeyframe(int time, float value)
    {
        Keyframe k = new Keyframe();
        k.time = time;
        k.value = value;
        keyframes.Add(k);
        keyframes.Sort(delegate(Keyframe a, Keyframe b) { return a.time.CompareTo(b.time); });
        lastFrame = (time > lastFrame) ? time : lastFrame;
    }

    public void Start()
    {
        timeline = 0;
        currentIndex = 0;
        run = true;
    }

    public void Update(GameTime gameTime, ref float value)
    {
        if (run)
        {
            timeline += gameTime.ElapsedGameTime.Milliseconds;
            value = MathHelper.SmoothStep(keyframes[currentIndex].value, keyframes[currentIndex + 1].value, (float)timeline / (float)keyframes[currentIndex + 1].time);
            if (timeline >= keyframes[currentIndex + 1].time && currentIndex != keyframes.Count) { currentIndex++; }
            if (timeline >= lastFrame) { run = false; }
        }
    }

    public struct Keyframe
    {
        public int time;
        public float value;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-01
    • 2014-04-15
    • 2012-09-09
    • 1970-01-01
    • 2010-09-25
    • 1970-01-01
    相关资源
    最近更新 更多