2048 的游戏开发者是如何让他们的棋子顺利移动的?看下面的细节

How did the game developer of 2048 made their tiles to move smoothly ? see the detail below

我已经制作了 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) yield 的第一个动作中执行的它首先为 null ?

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

2048 的来源表明,游戏的作者使用 CSS 转换来为方块的移动设置动画。 .tile class 具有以下属性,其中包括:

.tile {
    transition: 200ms ease-in-out;
    transition-property: transform;
}

这意味着,如果具有 .tile class 的元素的变换属性发生变化,它会平滑过渡,导致元素在屏幕上滑动。您可以了解有关 CSS 转换的更多信息 here

你的问题就这么简单,

ONLY AFTER MY COMPLETE MOVEMENT OF TILE...

i have been spending days to achieve this but i am not able to do how to stop executing the code ...

如果你想启动协程,但继续...

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中常见的基本错误。

不要忘记“产量 return”!

注意!

当然是这个代码

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) );

有道理吗?