如何停止对象并改变方向 [英] How to stop object and change direction

查看:58
本文介绍了如何停止对象并改变方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

我正在尝试制作一个程序:当按下按钮时对象向前移动,当你放开它时它停止。我无法理解如何制作它。有人可以帮帮我吗?



我尝试过的事情:



< pre lang =c#> private void button1_Click( object sender,EventArgs e)
{
for int i = 1 ; i < 300 ; i ++)
{
int x2 = i + 1 ;
int y2 = 100 ;
Graphics g2 = this .CreateGraphics();
g2.FillRectangle( new SolidBrush(Color.Black),x2,y2, 6 6 );
Invalidate();

int x1 = i-1;
int y1 = 100 ;
颜色c = .BackColor;
图形g1 = .CreateGraphics();
g1.FillRectangle( new SolidBrush(c),x1,y1, 6 6 );


System.Threading.Thread.Sleep( 50 );


}
}

解决方案

停止使用Thread。睡眠:这导致你的整个线程 - 在这种情况下,UI线程,处理显示和所有用户输入的所有工作 - 暂停。

即使它不是,那代码是Click事件处理程序中的一个循环。

这意味着屏幕不会更新,鼠标单击在您离开方法之前不起作用!



丢掉循环 - 一般来说Windows和特别是C#在控制台应用程序之外不起作用。

在表单中添加一个计时器。将Interval属性设置为100并启动它。

添加一个名为moving的类级别bool,如果为false则启动。

在Button单击事件中,反转开始您的移动变量。

处理计时器的Tick事件。

在处理程序中,选中移动。如果这是真的,那么在循环中执行一次代码,只执行一次。 )这意味着将 i 移动到班级。如果它是假的,什么都不做。



BTW:这不是最好的工作方式,而且实际上很危险。目前,在任何方法结束时,在您创建的所有图形项目上调用Dispose:

 g1.Dispose(); 
g2.Dispose();

如果你不这样做,你的应用程序会比你想象的更快崩溃...

有更好的方法,比如做Form Paint事件中的绘图代码,但我怀疑这是后来的。


Hello!
I'm trying to make a program: object moves forward while a button press and when you let go it stops.I can't understand how to make it. Can anybody help me?

What I have tried:

private void button1_Click(object sender, EventArgs e)
       {
           for (int i = 1; i < 300; i++)
           {
               int x2 = i + 1;
               int y2 = 100;
               Graphics g2 = this.CreateGraphics();
               g2.FillRectangle(new SolidBrush(Color.Black), x2, y2, 6, 6);
               Invalidate();

               int x1 = i-1;
               int y1 = 100;
               Color c = this.BackColor;
               Graphics g1 = this.CreateGraphics();
               g1.FillRectangle(new SolidBrush(c), x1, y1, 6, 6);


               System.Threading.Thread.Sleep(50);


           }
       }

解决方案

Stop using Thread.Sleep: that causes your whole thread - in this case the UI thread, which dos all the work of handling the display and all user input - to be suspended.
And even if it wasn't, that code is a loop inside a Click event handler.
Which means that the screen doesn't get updated and mouse clicks don't work until you leave the method!

Throw away the loop - Windows in general and C# in particular don't work like that outside of console applications.
Add a Timer to your form. Set the Interval property to 100 and start it.
Add a class level bool called "moving" and start if as false.
In your Button click event, invert the start of your "moving" variable.
Handle the Tick event for the timer.
In the handler, check "moving". If it's true, do your code inside the loop once, and once only. )This will mean moving i to class level as well. If it's false, do nothing.

BTW: That's not the best way to do the job, and it's actually dangerous. For the moment, at the end of any method, call Dispose on all the Graphics items you created:

g1.Dispose();
g2.Dispose();

If you don't, your application will crash sooner than you think...
There are betters ways, such as doing the drawing code in the Form Paint event, but that's for later, I suspect.


这篇关于如何停止对象并改变方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆