2048的游戏开发商是如何让他们的瓷砖顺利移动的?请参阅下面的详细信息



我已经制作了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) );

有道理吗?

2048的来源显示游戏作者使用CSS转换来动画化瓦片的移动。.tile类具有以下属性:

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

这意味着,如果具有.tile类的元素的transform属性发生更改,它将平滑转换,从而导致元素在屏幕上滑动。您可以在这里了解更多关于CSS转换的信息。

最新更新