在窗口窗体中中断跳转动画



到目前为止,我有一个跳跃动画。它使用整数"跳跃"来循环浏览创建跳跃动画的图像数组,此外,根据"跳跃"的数字,高度"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 秒调用一次此方法,以便在我按下指定的跳转键时绘制它......现在我的问题是假设我处于跳跃动画的中间,我想回去。我该怎么做。把它想象成双跳。此外,跳跃和高度(位置Y)直接相关。因此,如果跳跃为 0、1、2 或 3,则高度为 214 - (跳跃 + 1)* 30)。否则,如果跳跃是 (5-9) 高度是 94 + (跳跃 - 4) * 24)。因此,绘制任何图像的最大高度是 94。(它的窗口形式所以向上是向下,向下是向上...(0,0) 位于左上角)。

///

从视觉角度来看,这类似于我的跳跃动画。这个在时间方面有点短,但它是我能找到的最好的。

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

现在想象一下这个家伙是铁人三项,他用他的喷气助推器跳起来,但现在他还在空中,他决定直接上升。

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

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

响应单击空格键,您知道您必须创建一个JumpAnimation.但是,当您的计时器滴答作响时,您不想处理跳跃喷气背包动画的细节 - 您需要一个IAnimation界面,允许您继续动画,无论它是什么。当您激活喷气背包时,您只想用JetPackAnimation替换当前处于活动状态的任何动画:

在您的表格中:

private IAnimation currentAnimation = null;

IAnimation界面:

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

当您在JumpAnimation中实现IAnimation时,您可以重用您在问题中共享的许多代码。

现在,您可以创建一个类,而不是只返回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; }
...
}

我希望你明白这个想法。我并不是说这是解决你问题的唯一或最好的方法,但有时另一个人对这个问题的看法有助于跳出框框思考。

最新更新