动画左右移动

时间:2016-07-27 05:24:21

标签: c# winforms animation

我正在制作一个窗体动画,其中一个角色的动画向左走,然后一旦到达窗体的末尾就向右走。 并且我必须为动画停止播放/暂停按钮并继续他离开的地方

我每个角色左右走16帧

我想问一下如何在图片框上设置边框,以及如何让图片框一旦到达窗体的左边缘就改变为右行动画,并在它到达右边时改变左行动画窗口边缘

public partial class Form1 : Form
{
    //Declare a new integer for frame and set it to 1
    int frame = 1;

    public Form1()
    {
        InitializeComponent();
        timer1.Enabled = true;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        //Increase the frame per tick
        frame++;

        //Loop : If the frame exceeds 16, set the frame back to 1 
        if (frame > 16)
        {
            frame = 1;
        }
        //REtrieve the image fromfile base on the value of the ineger "frame"
        pictureBox1.Image = Image.FromFile(Application.StartupPath + 
                            "\\left" + frame + ".png");

        int x = pictureBox1.Location.X;
        int y = pictureBox1.Location.Y;
        x -= 5;
        //else if (e.KeyCode == Keys.Left) x -= 1;
        pictureBox1.Location = new Point(x, y);
    }

3 个答案:

答案 0 :(得分:0)

PictureBox有一个border属性。检查它是否适合你。

您可以检查PictureBox Left是否匹配左侧表格,这意味着它需要向右移动并在此条件下更改图像。

答案 1 :(得分:0)

要检测控件何时到达边界,您可以检查Location.X属性+宽度是否等于或大于PictureBox容器(在本例中为Form)的宽度。

if(pictureBox1.Location.X + pictureBox1.Width >= pictureBox1.parent.Width)
{
    direction = "left";//reached the end so go left

当然,您可以通过检查pictureBox1.Location.X == 0

来轻松测试我们是否处于起始位置

答案 2 :(得分:0)

您将需要2个不同的图像1指向左侧,1个指向右侧...
创建一个额外的变量说明方向
如果符合以下条件,您可以转动图像方向

if direction = right and picture.location >= (how far you want it to go probably border)

然后

  • direction = left
  • 更改左侧定向图像
  • 将您的运动变量更改为负数

    if direction = left and picture.location <= 0

    然后
  • direction = right
  • 更改为右向图像
  • 将您的运动变量更改为正为

    < / p>

  • 相关问题