我有两难选择。我正在尝试执行以下操作:我有3个按钮,单击一个按钮,它增加,单击另一个按钮,它增加,上一个按钮恢复到原始大小。
private void PigeonholesHasClicked(GameObject target, MouseEventType type)
{
if (type == MouseEventType.CLICK)
{
TargetIndex = System.Array.IndexOf(Pigeonholes, target);
PigeonholesTrasnform();
}
}
Vector3 mPosition;
int lastIndex = 0;
public float mIndex;
private void PigeonholesTrasnform()
{
mPosition = new Vector3(Pigeonholes[TargetIndex].transform.position.x - mIndex, Pigeonholes[TargetIndex].transform.position.y, 0f);
Pigeonholes[TargetIndex].transform.DOMove(mPosition, 0.5f);
Pigeonholes[TargetIndex].GetComponent<BoxCollider2D>().enabled = false;
if (TargetIndex != lastIndex)
{
mPosition = new Vector3(Pigeonholes[lastIndex].transform.position.x + mIndex, Pigeonholes[lastIndex].transform.position.y, 0f);
Pigeonholes[lastIndex].transform.DOMove(mPosition, 0.5f);
for (int i = 0; i < Pigeonholes.Length; i++)
{
if (i != TargetIndex)
Pigeonholes[i].GetComponent<BoxCollider2D>().enabled = true;
}
}
lastIndex = TargetIndex;
}
问题是,如果我单击1或2按钮,则0按钮向右移动0.4;如果我首先开始输入0,然后按1或2,那么一切正常。 lastIndex最初为0,如果我将其设为-1,则会给我一个错误。帮助!!!
答案 0 :(得分:1)
开始时,您尚未选择任何东西=>实际上,您实际上不希望最初是0
。
我起初会离开lastIndex = -1;
,而只是忽略这些行
mPosition = new Vector3(Pigeonholes[lastIndex].transform.position.x + mIndex, Pigeonholes[lastIndex].transform.position.y, 0f);
Pigeonholes[lastIndex].transform.DOMove(mPosition, 0.5f);
如果它仍然是-1
->表示之前未选择任何内容:
if(lastIndex >= 0)
{
mPosition = new Vector3(Pigeonholes[lastIndex].transform.position.x + mIndex, Pigeonholes[lastIndex].transform.position.y, 0f);
Pigeonholes[lastIndex].transform.DOMove(mPosition, 0.5f);
}