C# 在c语言中移动图像的问题#
我已经编写了一个应用程序,用C# 在c语言中移动图像的问题#,c#,winforms,C#,Winforms,我已经编写了一个应用程序,用PictureBox移动表单上的图像。但我的代码只是水平移动…我用了一个计时器 我需要将图像从初始点(例如X0,Y0)水平移动到精确位置(例如(Xc,Y0)),然后垂直向上或向下移动到(Xc,Ym),然后水平向后移动到(Xf,Ym) 我已经写了那个部分,移动图像水平达到(Xc,Y0),但我不知道如何写其他 下面是我从(X0,Y0)移动到(Xc,Y0)的代码: 此外,我做了一些尝试,但没有得到任何结果 以下是我的尝试(尝试更改方法计时器): void timer_Tic
PictureBox
移动表单上的图像。但我的代码只是水平移动…我用了一个计时器
我需要将图像从初始点(例如X0,Y0)水平移动到精确位置(例如(Xc,Y0)),然后垂直向上或向下移动到(Xc,Ym),然后水平向后移动到(Xf,Ym)
我已经写了那个部分,移动图像水平达到(Xc,Y0),但我不知道如何写其他
下面是我从(X0,Y0)移动到(Xc,Y0)的代码:
此外,我做了一些尝试,但没有得到任何结果
以下是我的尝试(尝试更改方法计时器):
void timer_Tick2(对象发送方,事件参数e)
{
int x=pictureBox1.Location.x;
int y=pictureBox1.Location.y;
如果(x 500)
{
如果(y键是使用时间而不是x作为键:
int t = 0;
void timer_Tick1(object sender, EventArgs e)
{
t++;
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
if (t <= 250)//go right 500 in 250 ticks
pictureBox1.Location = new Point(x + 2, y);
else if (t <= 500)//...then go down 250 in 250 ticks
pictureBox1.Location = new Point(x, y + 1);
else if (t <= 750)//...then go left 500 in 250 ticks
pictureBox1.Location = new Point(x - 2, y);
else
timer1.Stop();
}
int t=0;
无效计时器_Tick1(对象发送方,事件参数e)
{
t++;
int x=pictureBox1.Location.x;
int y=pictureBox1.Location.y;
if(t)我建议为时间创建一个新的变量t,它会在每一个刻度上递增。然后你可以从中计算x和y,它可能会更清晰。@Justin-你能解释更多吗?!@Eric Mickelsen-谢谢你的帮助…我猜我落入了第一个“if块”中.但我不完全理解你的建议,你能完成你的代码吗?@thehero:我用更多的代码进行了编辑。我决定用上一个点来定义新点,以便更符合你的想法。也可以用t来定义点(这可能是更好的方法)。由于你的运动从未两次穿过同一点,因此也可以在不引入t的情况下修复代码-但这样更好。@Eric Mickelsen-谢谢,我现在已经测试了你的代码,但图像只是向右移动并停在那里…问题出在哪里?!@thehero:你确定使用了正确的处理程序吗?你可以使用断点以确保调用了正确的代码。或者,您可以发布代码的完整列表。@Eric Mickelsen-抱歉…我发现了错误!!!您的代码是正确的!非常感谢您的帮助!
void timer_Tick2(object sender, EventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
if (x <= 500)
pictureBox1.Location = new Point(x + 2, y);
if (x > 500)
{
if (y <= 250)
pictureBox1.Location = new Point(x, y + 1);
if (y == 250)
{
pictureBox1.Location = new Point(x - 2, y);
if (x < 50)
timer1.Stop();
}
}
}
int t = 0;
void timer_Tick1(object sender, EventArgs e)
{
t++;
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
if (t <= 250)//go right 500 in 250 ticks
pictureBox1.Location = new Point(x + 2, y);
else if (t <= 500)//...then go down 250 in 250 ticks
pictureBox1.Location = new Point(x, y + 1);
else if (t <= 750)//...then go left 500 in 250 ticks
pictureBox1.Location = new Point(x - 2, y);
else
timer1.Stop();
}