2017-08-02 45 views
-1

我正在C#上编程Visual Studio 2016,并且正在制作游戏。我需要重复一个pictureBox。它应该从屏幕的右侧开始,然后移动到屏幕的左侧。如何在屏幕上重复图片框来模拟动画?

我该怎么做?目前,我有这样的代码:

while (pictureBox4.Location.X == -10 && pictureBox4.Location.Y == -2) 
{ 
    pictureBox4.Location = new Point(pictureBox4.Location.X - x, pictureBox4.Location.Y - y); 
} 

X & y是随机变量。

+0

我很惊讶,当看到你在Visual Studio 2016编程)您是指2015年或2017? –

+0

这是VS 2017,对不起 –

+0

'while(pictureBox4.Location.X == -10' should not that'> ='?? if'while'while while'。慷慨和add和'else'子句!当然,没有告诉我们代码从哪里来的都是猜测......! – TaW

回答

0

如果我正确地理解你的问题(其中,如果我没有请告诉我),你应该试试这个:

public static class PictureBoxExtension 
{ 
    public static void SetImage(this PictureBox pic, Image image, int repeatX) 
    { 
     Bitmap bm = new Bitmap(image.Width * repeatX, image.Height); 
     Graphics gp = Graphics.FromImage(bm); 
     for (int x = 0; x <= bm.Width - image.Width; x += image.Width) 
     { 
      gp.DrawImage(image, new Point(x, 0)); 
     } 
     pic.Image = bm; 
    } 
} 
+3

你在这里泄漏了'Bitmap'和'Graphics'对象。'Graphics'对象的创建应该包含在'using'语句中以防止那么,旧的'pic.Image'应该被明确地处置掉 –

+0

感谢您告诉我我会修复这个问题 –

+0

对不起,伙计它在我的程序中不起作用我需要做的是,picturebox重新开始屏幕右侧到达屏幕左侧时对不起我的英语 –