使用计时器移动图像

我编写了一个代码,用timerpictureBox从特定位置移动图像。

但是我的代码无法正常工作,因为图像不会在我想要的路径上移动。

我想要的是:

 - create images from points (10,Y0). - move horizontally right to reach point (500,Y0). - move vertically down or up to reach point (500,750) and stop here for example 7 second. - then, move again horizontally right and go to point (700,750) and stop here for example 8 second. - then, move again horizontally right and go to point (750,750). - at this point, if (complete == true) the `pictureBox` must hide and back to (10,Y0) - if (complete == false) then if (up == true) - move vertically up to reach point (750,900) and and - move horizontally right and go to (900,900) and stop here for example 10 second. - then, then, move again horizontally left and go to point (500,900). - then, move vertically down to reach point (500,750). else if (down == true) - move vertically down to reach point (750,600) and and - move horizontally right and go to (900,600) and stop here for example 10 second. - then, then, move again horizontally left and go to point (500,600). - then, move vertically up to reach point (500,750). 

我写了一些代码,但正如我所说,它没有正确移动……

还有一件事:我如何实现等待? (当达到exapmle(900,600)时,我们必须等待10秒或等到外面的东西允许图像移动)。

请帮我…

到目前为止,这是我的代码:

  private int k = 0; void timer_Tick(object sender, EventArgs e) { k++; int x = p.Location.X; int y = p.Location.Y; if (k <= 250) p.Location = new Point(x + 2, y); else if (k <= 400) { // if we are moving up of point (500,750) if (y  750) p.Location = new Point(x, y - 1); } // *** wait HERE. else if (k <= 500) p.Location = new Point(x + 2, y); // *** wait HERE. else if (k <= 550) p.Location = new Point(x + 2, y); else if (k <= 700) { if (complete == true) p.Location = new Point(x, y + 1); else p.Location = new Point(x, y - 1); } else if (k <= 800) p.Location = new Point(x + 2, y); // ***** wait HERE else if (k <= 1200) p.Location = new Point(x - 2, y); else if (k <= 1400) { if (complete == true) p.Location = new Point(x, y - 1); else p.Location = new Point(x, y + 1); } else timer1.Stop(); } } private void button1_Click(object sender, EventArgs e) { timer1.Start(); timer1.Interval = 15; timer1.Tick += new EventHandler(timer_Tick); } 

说实话,因为我不完全确定它实际上在做什么,所以我不打算提供一个图片盒解决方案。 根据我的经验,动画,计时等从来都不容易使用计时器实现,主要是因为你使用的计时器并不能保证它们应该在它们应该的时候准确地触发 – 因为它们需要Windows消息泵是空的要通过的计时器消息。 这不是不可能或任何事情,事实上我的第一部动画片也是用定时器完成的。

如果您正在编写的代码是为了满足计算机动画中的一些练习 (即家庭作业?)那么您将不得不学习这一点作为尝试解决问题的一部分或者更慷慨的人,我无疑会给您答案。

就我而言,在WPF中执行此操作并使用其动画和故事板function。

这样,您可以指定时间轴和路径(使用WPF简单的线性路径),然后您可以使用事件进行决策。 然后,管理实际的动画不再是您的问题 – 您只需指定需要的位置以及应该花多长时间。

由于WPF使用的“正确”渲染循环类似于游戏中使用的东西,因此最终结果在屏幕上也可能会出色。

对不起,如果这个答案没有帮助。