简体   繁体   English

如何使用 Visual Studio 在单击时切换图像

[英]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.

相关问题 如何使用Xamarin切换到Visual Studio的另一个页面? - How to switch to another page with xamarin for visual studio? 如何在Visual Studio中将图像插入图像数组? - How to insert images into image array in visual studio? 如何在 Visual Studio 扩展中显示图像? - How to display images in a Visual Studio extension? 如何在 Visual Studio 诊断工具窗口中切换进程? - How to switch process in Visual Studio Diagnostic Tools Window? 如何在 Visual Studio Code 上生成 switch 语句标签? - How to generate switch statement labels on Visual Studio Code? 在Visual Studio设计阶段如何在面板之间切换? - how to switch between panels during designing phase in visual studio? 如何在Visual Studio 2015中将DLL创建从ANSI切换到UNICODE - How to switch dll creation from ANSI into UNICODE in Visual Studio 2015 如何为Visual Studio项目添加和编译自定义“平台”开关? - How to add and compile for custom 'Platform' switch for visual studio projects? 如何在 Visual Studio 中切换 .NET Core 项目的目标框架 - How to switch between target frameworks for .NET Core projects in Visual Studio 如何在 Visual Studio 中自动刷新 switch 语句案例? - How to refresh switch statement cases automatically in Visual Studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM