Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 2048游戏开发者是如何让他们的游戏顺利运行的?详见下文_C#_Unity3d_Coroutine - Fatal编程技术网

C# 2048游戏开发者是如何让他们的游戏顺利运行的?详见下文

C# 2048游戏开发者是如何让他们的游戏顺利运行的?详见下文,c#,unity3d,coroutine,C#,Unity3d,Coroutine,我制作了2048游戏的完整副本,但我通过传送移动了瓷砖(没有像原始游戏那样平滑地移动瓷砖) 我使用了以下代码来计算移动瓷砖的平滑度 //GameManager script void MoveRight () { //some code .. AnimateTileMovement (newPosition); // newposition is the position to whihc the tiles is going to move //some cod

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

     }


 }
我已经花了几天的时间来实现这一点,但我无法停止执行
start例程(“TileMovement”,newPosition)
下面的代码,因为当
IEnumerator TileMovement(Vector3 newPosition)
第一次将其设为空时,代码会在第一次移动时执行

我读过这篇文章,也尝试过,但无法做到。请建议我怎么做。

游戏的作者使用CSS转换来设置瓷砖移动的动画。
.tile
类具有以下属性:

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

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

您的问题很简单

只有在我完全移动瓷砖之后

我已经花了几天的时间来实现这一点,但我无法做如何停止执行代码

如果你想开始一个合作项目,但要坚持下去

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

有道理吗?

好吧,这教会我更好地阅读问题。但是仍然回答了标题中的问题。是的,先生。我的问题解决了,非常感谢