Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#图片盒和计时器don';我们不能一起工作_C#_Timer_Drawing_Picturebox - Fatal编程技术网

C#图片盒和计时器don';我们不能一起工作

C#图片盒和计时器don';我们不能一起工作,c#,timer,drawing,picturebox,C#,Timer,Drawing,Picturebox,因此,我试图用C#制作一个简单的游戏,使用PictureBox进行绘制,使用定时器进行更新功能,但我注意到,当我开始在PictureBox上绘制时,定时器停止工作。。我不知道为什么 以下是picturebox代码: private void pictureBox1_Paint(object sender, PaintEventArgs e) { Console.WriteLine("PicBox running!"); if (this.MyGame.In

因此,我试图用C#制作一个简单的游戏,使用PictureBox进行绘制,使用定时器进行更新功能,但我注意到,当我开始在PictureBox上绘制时,定时器停止工作。。我不知道为什么

以下是picturebox代码:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        Console.WriteLine("PicBox running!");
        if (this.MyGame.InGame)
        {
            this.pictureBox1.Refresh();
            e.Graphics.Clear(Color.White);               
            MyGame.CurrentMap.Draw(e.Graphics);
            //e.Graphics.Dispose(); <- if I uncomment this, then the whole programm just freezes
        }
    }
下面是MyGame.CurrentMap.Draw(e.Graphics)调用的一个方法:


任何帮助都将不胜感激。我来自javascript,所以我真的不知道我在这里是否犯了严重的错误。

当计时器出现滴答声时,你想重新绘制你的PictureBox。为此,应使PictureBox接收绘制事件。这就是Refresh()的作用,因此调用This.pictureBox1.Refresh();在计时器1_滴答声的末尾


绘制事件包含刷新调用是没有意义的,因为这反过来会生成绘制事件。

请演示如何初始化计时器。我认为您需要在另一个线程中绘制。this.pictureBox1.Refresh();不应该出现在图画里,而是在时间的尽头。@Zoyd我爱你,伙计!!现在效果很好。我已经花了大约两个小时试图弄明白这一点。。你能解释一下为什么它必须在计时器的末尾吗?我同意Gleb的观点,如果你的计时器和你的私有void pictureBox1_Paint(对象发送器,PaintEventArgs e)在同一个线程中,一旦你在图片框中绘制,你就转到pictureBox1_Paint方法并停止所有其他方法,因为picturebox1\u Paint是一个事件处理程序。。据我所见。
private void timer1_Tick(object sender, EventArgs e)
    {
        Console.WriteLine("Timer running!");
        if (this.MyGame.InGame)
        {
            MyGame.CurrentMap.Update();
            MyGame.UpdateTime();                
        }
    }
public void Draw(Graphics g)
    {
        foreach(Planet item in Entities){
            item.Draw(g);
        }            
    }