gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:49:10 25 4
gpt4 key购买 nike

我制作了 2048 游戏的完整副本,但我通过传送移动了方 block (没有像在原始游戏中那样平滑地移动方 block )

我使用以下代码来平滑移动瓷砖。

//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

最佳答案

你的问题就这么简单,

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

不要忘记“ yield 返回”!

注意!

当然是这段代码

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

有道理吗?

关于c# - 2048 的游戏开发者是如何让他们的棋子顺利移动的?看下面的细节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37163664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com