以Windows格式中断跳转动画

时间:2017-05-29 12:08:00

标签: c# winforms animation

到目前为止,我有一个跳跃动画。它使用整数“跳转”来循环创建跳转动画的图像数组,另外,根据“跳跃”的数量,高度“PositionY”将增加或减少,当跳跃命中10时,动画结束.....这是跳转动画代码

    public Bitmap Jump_Frame2Draw()
    {
        Jump_Frames = new Bitmap[] { Jump_1, Jump_2, Jump_3, Jump_4, Jump_5, Jump_6, Jump_7,Jump_7,
        Jump_8, Jump_8 };
        if (jump < Jump_Frames.Length)
        {
            if (jump <= 3)
            {
                PositionY -= 30;
            }
            else if (jump == 8 || jump == 10)
            {
                PositionY += 0;
            }
            else
            {
                PositionY += 24;
            }
            jumpFrame = Jump_Frames[jump];
            jump++;
            if (jump == 10)
            {
                jumpTimer.Stop();
                isJumping = false;
                jump = 0;
                standTimer.Start();
                Invalidate();
            }
        }
        tracer = jumpFrame.GetPixel(1, 1);
        jumpFrame.MakeTransparent(tracer);
        isJumping = true;
        return jumpFrame;
    }

通过使用计时器和绘画事件,我可以每隔x秒调用此方法,以便在按下指定的跳转键时绘制它...现在我的问题是我可以说我在中间跳跃动画,我想回去。我该怎么办呢。把它想象成双跳。此外,跳跃和高度(PositionY)直接相关。因此,如果跳跃是0,1,2或3,那么高度是214 - (跳跃+ 1)* 30)。否则如果跳跃是(5-9)高度是94 +(跳跃 - 4)* 24)。所以任何图像的最大高度都是94.(它的窗口形状向上和向下都是向上......(0,0)位于左上方。)

///////////

对于视觉透视,这与我的跳跃动画类似。这个时间稍微短一点,但它是我能找到的最好的。

跳转动画:https://media.giphy.com/media/wXervlFEqohO0/giphy.gif

现在想象这个家伙是铁人,他用他的喷气式增压器跳起来,但现在他还在空中,他决定直接上去。

1 个答案:

答案 0 :(得分:2)

如果您将大多数特定于动画的代码移动到专用的JumpAnimation类,我认为您可以省去很多麻烦。传递构造函数中特定动画所需的所有信息:

class JumpAnimation
{
    public JumpAnimation(int howHigh)
    {
        ...
    }
}

响应空格键上的点击,您知道必须创建JumpAnimation。但是当你的计时器正在滴答作响时,你不想处理 jump jetpack 动画的细节 - 你想要一个允许你的IAnimation界面无论它是什么,继续动画。当你启动jetpack时,你只想用JetPackAnimation替换当前活动的任何动画:

以您的形式:

private IAnimation currentAnimation = null;

IAnimation界面:

public interface IAnimation
{
    // get the bitmap at the time relevant to the animation start
    Bitmap GetBitmapAt(int time); 
}

IAnimation中实施JumpAnimation时,您可以重复使用您在问题中分享的大量代码。

现在,您可以创建一个包含有关“动画中当前步骤”的更多信息的类,而不是只返回Bitmap

public class AnimationStep
{
    public Bitmap Bitmap { get; set; }
    // the y-offset
    public int OffsetY { get; set; }
    // indicates whether this was the last step of the animation
    public bool Completed { get; set; }
    // a jump animation can be interrupted by a jetpack animation, but a DieAnimation cant:
    public bool CanBeInterrupted { get; set; }
    ...
}

我希望你明白这个主意。我并不是说这是解决问题的唯一或最佳方式,但有时候另一个人对此事的看法有助于思考问题。