2048年的游戏开发者是如何让他们的瓷砖顺利移动的? 看下面的详细信息

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

我使用以下代码来移动瓷砖的smothness。

//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)下面的代码StartCoroutine ("TileMovement", newPosition)因为代码在IEnumerator TileMovement(Vector3 newPosition)第一个动作执行时先null?

我已经阅读了这篇文章并尝试过但无法这样做请建议我做什么做Coroutines团结问

你的问题很简单,

仅在我完成TILE运动之后……

我花了好几天才实现这个目标,但我无法做到如何停止执行代码……

如果你想开始一个协程, 但继续 ……

 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过渡的更多信息