抱歉,如果问题看起来含糊不清,但我正在创建一个基于回合的 2D roguelike 游戏用于编码练习,并希望玩家能够在用户单击按钮时移动两次而敌人移动一次。我尝试使用多个布尔值,但无济于事。
玩家回合是一个整数,一旦达到 0,就成为敌人回合。用户单击按钮时的代码:
public void ButtonClicked()
{
GameManager.instance.playersTurn = 2;
}
我的播放器管理器中的移动代码(这是错误所在)
void Update () {
if (0 > GameManager.instance.playersTurn)
return;
horizontal = (int)Input.GetAxisRaw ("Horizontal");
vertical = (int)Input.GetAxisRaw ("Vertical");
//dont allow vertical movement
if (horizontal != 0)
vertical = 0;
//if a key is pressed:
if (vertical != 0 || horizontal != 0)
AttemptMove<Wall> (horizontal, vertical);
}
protected override void AttemptMove <T>(int xDir, int yDir)
{
food--;
foodText.text = "Food: " + food;
base.AttemptMove<T> (xDir, yDir);
CheckIfGameOver ();
GameManager.instance.playersTurn--;
}
GameManager.instance.playersTurn--;
即使我只移动了一次,也会减少到零。如何每次移动减少一次?我认为问题在于,这是在更新中调用的。任何指针都会有很大的帮助,谢谢大家。
这是我父类中的其他代码,以防错误可能出在此处:
protected bool Move(int xDir, int yDir, out RaycastHit2D hit)
{
Vector2 start = transform.position;
Vector2 end = start + new Vector2 (xDir, yDir);
boxCollider.enabled = false;
hit = Physics2D.Linecast (start, end, blockingLayer);
boxCollider.enabled= true;
if (hit.transform == null) {
StartCoroutine (SmoothMovement (end));
return true;
}
return false;
}
protected virtual void AttemptMove<T> (int xDir, int yDir)
where T: Component
{
RaycastHit2D hit;
bool canMove= Move (xDir, yDir, out hit);
if (hit.transform == null)
return;
T hitComponent = hit.transform.GetComponent<T> ();
if (!canMove && hitComponent != null)
OnCantMove (hitComponent);
}
protected IEnumerator SmoothMovement(Vector3 end)
{
float sqrRemainingDistance = (transform.position - end).sqrMagnitude;
while (sqrRemainingDistance > float.Epsilon) {
Vector3 newPosition = Vector3.MoveTowards (rb2d.position, end, inverseMoveTime * Time.deltaTime);
rb2d.MovePosition (newPosition);
sqrRemainingDistance = (transform.position - end).sqrMagnitude;
yield return null;
}
}