简体   繁体   English

使用列表的IF语句<Bitmap>

[英]IF statement using a List<Bitmap>

So I have looked through a few previous solutions but thus far have not seen any to fix my issue. 因此,我已经浏览了一些以前的解决方案,但到目前为止,还没有任何解决方案可以解决我的问题。 I am trying to change a player's avatar when they click the picturebox. 当他们单击图片框时,我试图更改玩家的化身。 I have a list of images like so : 我有这样的图像列表:

List<Bitmap> avatars = new List<Bitmap>();

private void GameForm_Load(object sender, EventArgs e)
{
  avatars.Add(Properties.Resources.Head1);
  avatars.Add(Properties.Resources.Head2);
  avatars.Add(Properties.Resources.Head3);
  avatars.Add(Properties.Resources.Head4);
}

And this is where I am trying to change it at : 这是我尝试在以下位置进行更改的地方:

private void pictureBoxAvatar1_Click(object sender, EventArgs e)
{

  if(pictureBoxAvatar1.Image == avatars.ElementAt(0))
  {
    pictureBoxAvatar1.Image = avatars.ElementAt(1);
  }
  //I have four total possibilities, but just have this one statement until I figure it out

}

My issue is that I cannot find a condition to evaluate to switch the image. 我的问题是我找不到条件来评估切换图像。

There are problems with your approach which if changed might make things easier. 您的方法存在问题,如果更改,可能会使事情变得更容易。 First is that for each avatar you store a copy of the Image when you only need to store one copy per image. 首先是当您只需要为每个头像存储一个副本时,为每个头像存储一个副本。 While this is fine in small programs it will not scale well. 虽然这在小型程序中很好,但扩展性不好。 While the compiler might be smart enough to store each of the images only once, you run a big chance of creating a lot of redundant data and we shouldn't trust the compiler to handle this for us. 尽管编译器可能足够聪明,只能存储每个图像一次,但您很有可能会创建大量冗余数据,我们不应该相信编译器会为我们处理这些数据。 Of course this might change our render approach a little but it should improve caching and allow the program to run a little faster. 当然,这可能会稍微改变我们的渲染方法,但它应该改善缓存并允许程序运行更快。

The better solution would be to store the Images in one central location and for the avatars store a single pointer indicating which Images to use. 更好的解决方案是将图像存储在一个中央位置,而对于化身则存储一个指示要使用哪些图像的指针。

I would change pictureBoxAvatar1.Image to something along the lines of pictureBoxAvatar1.ImagePointer and then your function should become something like 我将pictureBoxAvatar1.Image更改为与pictureBoxAvatar1.ImagePointer相似的东西,然后您的函数pictureBoxAvatar1.ImagePointer类似

private void pictureBoxAvatar1_Click(object sender, EventArgs e)
{
   pictureBoxAvatar1.ImagePointer = (pictureBoxAvatar1.ImagePointer + 1) % avatars.size(); 
}

Something like 就像是

int avIndex = avatars.IndexOf(picturebox1.image);
avIndex++;
if (avIndex >= avatars.Count)
{ 
  avIndex = 0;
}
picturebox1.image = avatars[avIndex];

or equivalent depending on what avatars is. 或等价物,取决于化身是什么。

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

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