很抱歉,如果这个问题看起来很模糊,但我正在创建一个基于回转的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;
}
}