我有一个难题。我正在尝试做类似的事情:我有 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,它会给我一个错误。帮助!!!
1 回答
撒科打诨
TA贡献1934条经验 获得超2个赞
当您开始时,您还没有选择任何内容=>您实际上不希望它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);
}
- 1 回答
- 0 关注
- 129 浏览
添加回答
举报
0/150
提交
取消