繁体   English   中英

使用列表的IF语句<Bitmap>

[英]IF statement using a List<Bitmap>

因此,我已经浏览了一些以前的解决方案,但到目前为止,还没有任何解决方案可以解决我的问题。 当他们单击图片框时,我试图更改玩家的化身。 我有这样的图像列表:

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);
}

这是我尝试在以下位置进行更改的地方:

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

}

我的问题是我找不到条件来评估切换图像。

您的方法存在问题,如果更改,可能会使事情变得更容易。 首先是当您只需要为每个头像存储一个副本时,为每个头像存储一个副本。 虽然这在小型程序中很好,但扩展性不好。 尽管编译器可能足够聪明,只能存储每个图像一次,但您很有可能会创建大量冗余数据,我们不应该相信编译器会为我们处理这些数据。 当然,这可能会稍微改变我们的渲染方法,但它应该改善缓存并允许程序运行更快。

更好的解决方案是将图像存储在一个中央位置,而对于化身则存储一个指示要使用哪些图像的指针。

我将pictureBoxAvatar1.Image更改为与pictureBoxAvatar1.ImagePointer相似的东西,然后您的函数pictureBoxAvatar1.ImagePointer类似

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

就像是

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

或等价物,取决于化身是什么。

暂无
暂无

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

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