[英]How to switch images on click with Visual Studio
I'm using visual studios and I want to make a sprite look like it's walking.我正在使用视觉工作室,我想让一个精灵看起来像在走路。 So when I press the left arrow key the sprite moves left but I want to change the image each time I press the key so his other leg is forward and it seems like he is walking.
因此,当我按下左箭头键时,精灵会向左移动,但我想在每次按下键时更改图像,以便他的另一条腿向前,看起来他正在走路。 At first, I had a gif but it doesn't have the same effect.
起初,我有一个 gif,但它没有相同的效果。
I'm using visual studios and only new but can't seem to find an updated answer.我正在使用视觉工作室,只有新的,但似乎找不到更新的答案。 If there is anyways to improve my question please let me know.
如果有任何方法可以改善我的问题,请告诉我。 Thank you!
谢谢!
I'm Using Windows Form App (.NET Framework)我正在使用 Windows 表单应用程序(.NET 框架)
what is the code to change the image
更改图像的代码是什么
If you want to traverse through the pictures in the ImageList, you can define an field index
to store the index of images.如果要遍历ImageList中的图片,可以定义一个字段
index
来存储图片的索引。
Please refer to the following code.请参考以下代码。
int index = 0;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
if (index > imageList1.Images.Count - 1)
index = 0;
pictureBox1.Image = imageList1.Images[index++];
}
}
The test result,测试结果,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.