我已经制作了2048游戏的完整副本,但是我通过传送移动了瓷砖(没有像原始游戏中那样平滑移动瓷砖)

我使用以下代码来实现移动瓷砖的平滑度。

//GameManager script
 void MoveRight () {
     //some code ..
     AnimateTileMovement (newPosition); // newposition is the position to whihc the tiles is going to move
     //some code which i need to execute (ONLY AFTER MY COMPLETE MOVEMENT OF TILE)
     // BUT AS SOON AS TileMovement return its first null this code execute which is creating a lot of problem , what should i do ?
     //to stop execution these code until the tiles have moved towards its desired newPosition
 }

 //TilesMovement Script

 public void AnimationTileMovement(Vector3 newPosition) {
     StartCoroutine ("TileMovement", newPosition);

 }

 IEnumerator TileMovement(Vector3 newPosition) {
     while (transform.position != newPosition) {
         transform.position = Vector3.MoveTowards (transform.position, newPosition, speedTile * Time.deltaTime);
         yield return null;

     }


 }


我已经花了几天的时间来实现这一目标,但是我无法做如何停止执行StartCoroutine ("TileMovement", newPosition)下面的代码的方法,因为当IEnumerator TileMovement(Vector3 newPosition)首先将其设为null时,代码会在第一次移动时执行?

我已经阅读了这篇文章,也尝试过但无法这样做,请建议我该怎么做Coroutines unity ask

最佳答案

您的问题很简单,


  仅在我完全移动瓷砖之后...





  我已经花了几天的时间来实现这一目标,但我不能做如何停止执行代码...


如果您要启动协程,请继续...

Debug.Log("we're at A");
StartCoroutine("ShowExplosions");
Debug.Log("we're at B");


它将打印“ A”,并开始爆炸动画,但是它们立即继续前进并打印“ B”。 (它将打印“ A”,然后立即打印“ B”,爆炸将在打印“ B”的同时开始运行,然后继续进行。)

但是....

如果您要启动协程,请等待...

Debug.Log("we're at A");
yield return StartCoroutine("ShowExplosions");
Debug.Log("we're at B");


那将打印“ A”。然后它将开始爆炸并等待:它将不会执行任何操作,直到爆炸完成。只有这样,它才会打印“ B”。

这是Unity中经常出现的基本错误。

不要忘了“收益回报”!

注意!

当然,这段代码

Debug.Log("we're at A");
yield return StartCoroutine("ShowExplosions");
Debug.Log("we're at B");


必须本身在协程中。所以你会有类似...

public IEnumerator AnimationTileMovement(...)
   {
   Debug.Log("we're at A");
   .. your pre code
   yield return StartCoroutine("ShowExplosions");
   Debug.Log("we're at B");
   .. your post code
   }


在MoveRight中,您将拥有

public void MoveRight(...)
   {
   StartCoroutine( AnimateTileMovement(newPosition) );


说得通?

07-24 09:51
查看更多