C# Unity-Zelda life系统在更新hp容器时崩溃
我用5个健康容器建立了一个塞尔达健康系统。每个容器有4件 更新此healthbar时,我得到一个超出范围的异常 我的代码:C# Unity-Zelda life系统在更新hp容器时崩溃,c#,unity3d,C#,Unity3d,我用5个健康容器建立了一个塞尔达健康系统。每个容器有4件 更新此healthbar时,我得到一个超出范围的异常 我的代码: [SerializeField] Image[] healthContainers; // 5 containers as images int currentHealth; int maxHealth; int healthPerHealthContainer = 4; // 1 healthcontainer = 4 health pieces private vo
[SerializeField]
Image[] healthContainers; // 5 containers as images
int currentHealth;
int maxHealth;
int healthPerHealthContainer = 4; // 1 healthcontainer = 4 health pieces
private void Start()
{
maxHealth = healthContainers.Length * healthPerHealthContainer; // set max hp
currentHealth = maxHealth; // init current hp
UpdateHealthBar(); // first gui update
}
public void ChangeHealth(int amount) // get damage or heal
{
currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth); // add or subtract health
UpdateHealthBar(); // update gui
}
public void UpdateHealthBar() // gui updater
{
int healthContainerIndex = currentHealth / healthPerHealthContainer; // get the current heart to change
int healthContainerFillAmount = currentHealth % healthPerHealthContainer; // the fillamount of the heart
if (currentHealth % healthPerHealthContainer == 0)
{
if (currentHealth == healthContainers.Length)
{
healthContainers[healthContainerIndex - 1].fillAmount = 1;
return;
}
if (healthContainerIndex > 0)
{
healthContainers[healthContainerIndex].fillAmount = 0;
healthContainers[healthContainerIndex - 1].fillAmount = 1;
}
else
{
healthContainers[healthContainerIndex].fillAmount = 0;
return;
}
}
healthContainers[healthContainerIndex].fillAmount = healthContainerFillAmount / (float)healthPerHealthContainer;
}
因此,当使用5个健康容器,每个健康容器4个时,我从20马力开始。更新gui时,它会在
healthContainers[healthContainerIndex].fillAmount = 0;
因为
int healthContainerIndex = currentHealth / healthPerHealthContainer;
导致
healthContainerIndex = 5;
包含5个容器的数组的索引范围为0到4
但仅仅是写作
healthContainers[healthContainerIndex -1].fillAmount = 0;
这是错误的,因为我需要得到正确的心脏。有人知道我在这里犯的错误吗?我看不出来。我完全同意Jon Skeet的观点,把它写在一张纸上,这确实有助于解决像这样的索引问题 尽管如此,您可能需要重新考虑您的解决方案是否能按您希望的方式工作。你的解决方案只适用于假设玩家的生命值一次只上升或下降1个生命值的情况。如果玩家受到足够的伤害或完全恢复健康,你的解决方案可能会跳过一步,心脏可能无法正确更新。如果您只是迭代所有容器并更新它们,那么实现起来就会容易得多,而且更加健壮
public void UpdateHealthBar() // gui updater
{
for (int healthContainerIndex = 0; healthContainerIndex < healthContainers.Length; healthContainerIndex++)
{
healthContainers[healthContainerIndex].fillAmount = Mathf.Clamp01((float)currentHealth / healthPerHealthContainer - healthContainerIndex);
}
}
public void UpdateHealthBar()//gui更新程序
{
对于(int-healthContainerIndex=0;healthContainerIndex
绝对同意Jon Skeet将其写在一张纸上的观点,这确实有助于解决此类索引问题
尽管如此,您可能需要重新考虑您的解决方案是否能按您希望的方式工作。你的解决方案只适用于假设玩家的生命值一次只上升或下降1个生命值的情况。如果玩家受到足够的伤害或完全恢复健康,你的解决方案可能会跳过一步,心脏可能无法正确更新。如果您只是迭代所有容器并更新它们,那么实现起来就会容易得多,而且更加健壮
public void UpdateHealthBar() // gui updater
{
for (int healthContainerIndex = 0; healthContainerIndex < healthContainers.Length; healthContainerIndex++)
{
healthContainers[healthContainerIndex].fillAmount = Mathf.Clamp01((float)currentHealth / healthPerHealthContainer - healthContainerIndex);
}
}
public void UpdateHealthBar()//gui更新程序
{
对于(int-healthContainerIndex=0;healthContainerIndex
我建议你(在一张纸上)写下currentHealth
的值和你期望的healthContainerIndex
是什么,以及你期望更改的healthContainers
索引。几乎没有理由统一使用[SerializedField]。@Fattie well writing public。。。不是很好。将变量设置为private似乎更干净我建议您(在一张纸上)写下currentHealth
的值,以及您希望healthContainerIndex
是什么,以及您希望更改的healthContainers
索引。几乎没有理由使用[Serialized Field]在团结中,我写得很好。。。不是很好。将变量设置为private似乎更干净