C#某些类型的错误,我需要一些帮助 [英] C# Some type of error and i need some help

查看:114
本文介绍了C#某些类型的错误,我需要一些帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

This is my code

// the death movment
private void timer2_Tick(object sender, EventArgs e)
{
    // level 1
    if (label2.Text == "This Is Level 1")
    {
        if (Death.Width == 64)
        {
            Death.Location = new Point(Death.Location.X + 5, Death.Location.Y);
        }

        if (Death.Location == new Point(390, 202))
        {
            Death.Width = 63;
        }
        if (Death.Width == 63)
        {
            Death.Location = new Point(Death.Location.X - 5, Death.Location.Y);
        }

        if (Death.Location == new Point(30, 202))
        {
            Death.Width = 64;
        }
    }

    // level 2 this is the error
    if (label2.Text == "This Is Level 2")
    {
        if (Death.Width == 64)
        {
            Death.Location = new Point(Death.Location.X + 15, Death.Location.Y);
        }

        if (Death.Location == new Point(390, 202))
        {
            Death.Width = 63;
        }
        if (Death.Width == 63)
        {
            Death.Location = new Point(Death.Location.X - 15, Death.Location.Y);
        }

        if (Death.Location == new Point(30, 202))
        {
            Death.Width = 64;
        }
    }

}



因此,当我使它达到2级时,当我按下按钮启动计时器时,它应该会移动得更快,但是由于某些原因,图片框仅在X的随机位置冻结了.

我的意思是按钮更改了文本,表单开始时计时器已经在运行



So when I make it so it hits level 2 it should be moving faster when i hit a button to start the timer, but for some reason the picturebox just freezes in random places alone the X.

I mean the button changes the text, the timer is already going when the form starts

推荐答案

可能的优化:

Possible optimization:

// the death movement
private void timer2_Tick(object sender, EventArgs e)
{
    int movement = (label2.Text == "This Is Level 1") ? 5 : 15;
    if (Death.Width == 64)
    {
        Death.Location = new Point(Death.Location.X + movement, Death.Location.Y);
    }
    if (Death.Location == new Point(390, 202))
    {
        Death.Width = 63;
    }
    if (Death.Width == 63)
    {
        Death.Location = new Point(Death.Location.X - movement, Death.Location.Y);
    }
    if (Death.Location == new Point(30, 202))
    {
        Death.Width = 64;
    }
}



您还在使用计时器吗?如果您不希望计时器在窗体启动时运行,请不要创建或启动计时器,直到用户单击开始"按钮:



You still use timers? If you don''t want the timer to be running when the form starts, don''t create or start it until the user clicks the start button:

private void buttonStart_Click(...)
{
    if (m_timer2 == null)
    {
        m_timer2 = new System.Timers.Timer(25);
        m_timer2.Elapsed += new ElapsedEventHandler(timer2_Tick);
    }
    else
    {
        m_timer.Stop();
    }
    m_timer.Start();
}


如果您的计时器在表单启动时启动,则可能意味着您在设计器中将Timer.Enabled属性设置为true.而是在设计器中将其保留为false,并在用户单击按钮时将其设置为true.

约翰是对的,不要复制/粘贴代码,因为它很难维护.而且测试逻辑对我来说似乎很奇怪:您使用硬编码的点并使用=进行比较,但是您可以确定盒子将到达那个精确点吗?

我建议您改为使用>=<=测试坐标.这将使您的代码更健壮.

不要将标签文本与硬编码字符串进行比较.使用boolenum知道您所处的状况.

而且,如果您像约翰建议的那样使用2个不同的计时器,别忘了停止上一个计时器.
If your timer starts when you form starts, it probably means you set the Timer.Enabled property to true in the designer. Instead, leave it to false in the designer and set it to true when the user clicks the button.

John is right, don''t copy/paste code because it will be harder to maintain. And the tests logic seems strange to me: you use hard coded points and compare them using =, but can you be sure the box will reach that exact point?

I suggest you test coordinates with >= or <= instead. This will make your code robuster.

Don''t compare your label text to a hard coded string. Use a bool or enum to know what situation you are in.

And don''t forget to stop the previous timer if you use 2 different timers as John suggests.


如果这是某种动画动作,那么最糟糕的是使用.该计时器非常不准确,以至于即使没有武装的人眼也可以看到实际观察到的速度中无法接受的错误.实际上,对于所有类型的动画,这都是绝对不能接受的.您最好使用System.Timers.Timer,但即使那样也不是最好的方法.

真正首选的解决方案是线程.确保仅通过System.Windows.Threading.Dispatcher.BeginInvokeSystem.Windows.Threading.Dispatcher.InvokeSystem.Windows.Forms.Control.BeginInvokeSystem.Windows.Forms.Control.Invoke方法从此线程访问UI.根据您的设计,您可以使用通过其构造函数创建的常规System.Threading.Thread,来自线程池或System.ComponentModel.BackgroundWorker的线程.

—SA
If this is some kind of animated motion, the worst you could do is using System.Windows.Forms.Timer. This timer is so inaccurate that even an unarmed human eye can see unacceptable errors in actually observed speed. Practically for all kinds of animation this is absolutely unacceptable. You could better use System.Timers.Timer but even this is not the best.

The really preferred solution is thread. Make sure you access UI from this thread only via the methods System.Windows.Threading.Dispatcher.BeginInvoke, System.Windows.Threading.Dispatcher.Invoke, System.Windows.Forms.Control.BeginInvoke or System.Windows.Forms.Control.Invoke. Depending on your design, you can use a regular System.Threading.Thread created through its constructor, a thread from a thread pool or System.ComponentModel.BackgroundWorker.

—SA


这篇关于C#某些类型的错误,我需要一些帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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