我已经制作了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
答案 0 :(得分:4)
你的问题很简单,
仅在完成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) );
有道理吗?
答案 1 :(得分:-1)