简体   繁体   English

C#如何移动图片框,该框根据hes面对的方向更改图像?

[英]C# How do I move a picturebox that changes images depending on the direction hes facing?

Using visual studio 2015 I use a picture box as a player and have it move up, down, left and right. 使用Visual Studio 2015,我将图片框用作播放器,并使其上下左右移动。 When the picture box moves it shrinks and then looks like it teleports. 当图片框移动时,它会收缩,然后看起来像是在传送。 That is not what it is supposed to do. 那不是应该做的。 How do I properly get the picture box to change whenever I press the wasd keys? 每当我按下wasd键时,如何正确更改图片框?

if (e.KeyCode == Keys.D)
{
    x += 6;
    playerBox.Image = Properties.Resources.playerRight;
}

//moves player right and changes the image //向右移动玩家并更改图像

Just add to / subtract, the picturebox Top and Left: 只需添加/减去图片框的顶部和左侧:

if (e.KeyCode == Keys.D)
{
    playerBox.Left += 6;
    playerBox.Image = Properties.Resources.playerRight;
}

also to avoid changing photo everytime if the direction has not changed you may do something like this: 另外,如果方向不变,也可以避免每次更改照片,您可以执行以下操作:

if (e.KeyCode == Keys.D)
{
    playerBox.Left += 6;
    if((Keys)playerBox.Tag!=e.KeyCode)
    playerBox.Image = Properties.Resources.playerRight;
}

You must of course set some inital value for playerBox.Tag or you will get an error as it cant be cast to Keys 当然,您必须为playerBox.Tag设置一些初始值,否则会因为无法转换为Keys产生错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM