您好我成功制作了一个通过按键移动图片框的程序,但是图片框的移动并不平滑,当您第一次开始移动图片框时会出现延迟。
这是我的代码
namespace Imagebox_test1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.KeyPreview = true;
this.KeyDown += Form1_KeyDown;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
bool player_left;
bool player_right;
player_left = false;
player_right = false;
int move_speed;
move_speed = 10;
switch (e.KeyCode)
{
case Keys.A:
player_right = false;
player_left = true;
break;
case Keys.D:
player_right = true;
break;
}
if (player_left == true) x -= move_speed;
if (player_right == true) x += move_speed;
pictureBox1.Location = new Point(x, y);
}
}
}
如何摆脱这种滞后并使运动更顺畅?
非常感谢。