提问人:Lordvanan 提问时间:10/27/2023 更新时间:10/27/2023 访问量:44
为什么我的协程不会多次运行?
Why won't my coroutine run more than once?
问:
在我的游戏中,玩家可以拿起的道具,首先要检查角色是否已经道具。如果没有,那么它会启动名为“FireBreathing”的协程,该协程处理通电的影响。如果它们已经通电,我想停止协程并重新启动它,以免多个相同的协程重叠。
在我看来,我应该可以工作,但事实并非如此。它将像往常一样通过协程进行播放,但是当它完成时(要么只是等待协程完成,要么通过再次通电以重新启动循环),协程将不会再次启动。
type herepublic class StrawbiliEffect : MonoBehaviour
{
[SerializeField] float strawbiliMoveSpeed = 5f;
[SerializeField] float strawbiliJumpForce = 12f;
[SerializeField] float strawbiliDuration = 3f;
public bool isStrawbiliBoy;
IEnumerator coroutine;
AltPlayerController playerController;
Animator animator;
void Start()
{
playerController = GetComponent<AltPlayerController>();
coroutine = FireBreathing();
}
public void StrawbiliPowerUp()
{
Debug.Log("Function Called");
if (isStrawbiliBoy == false)
{
StartCoroutine(coroutine);
}
else if (isStrawbiliBoy == true)
{
StopCoroutine(coroutine);
Debug.Log("Coroutine stop attempted");
StartCoroutine(coroutine);
}
}
IEnumerator FireBreathing()
{
Debug.Log("Coroutine has started");
isStrawbiliBoy = true;
playerController.isNormalGerm = false;
playerController.moveSpeed = strawbiliMoveSpeed;
playerController.jumpForce = strawbiliJumpForce;
//animator.SetBool ("isStrawbiliBoy", true);
yield return new WaitForSeconds (strawbiliDuration);
//animator.SetBool ("isStrawbiliBoy", false);
playerController.isNormalGerm = true;
playerController.moveSpeed = playerController.normalMoveSpeed;
playerController.jumpForce = playerController.normalJumpForce;
isStrawbiliBoy = false;
Debug.Log("Coroutine has finished");
}
}
我移动以收集电源,控制台打印“调用的函数”,因此我知道拾取方法仍然正常工作。我已经检查以确保所有布尔变量都切换回正确的状态。我只是不知道它有什么问题。
答:
1赞
Evgenii Glebov
10/27/2023
#1
问题是,在第二次调用之前,协程。MoveNext() 将返回 false。由于 IEnumerator 已到达末尾。
简而言之,您需要以不同的方式使用它。
public class StrawbiliEffect : MonoBehaviour
{
[SerializeField] float strawbiliMoveSpeed = 5f;
[SerializeField] float strawbiliJumpForce = 12f;
[SerializeField] float strawbiliDuration = 3f;
public bool isStrawbiliBoy;
// CHANGED.
Coroutine coroutine;
AltPlayerController playerController;
Animator animator;
void Start()
{
playerController = GetComponent<AltPlayerController>();
//coroutine = FireBreathing();
}
// CHANGED.
public void StrawbiliPowerUp()
{
Debug.Log("Function Called");
if (isStrawbiliBoy == false)
{
if (coroutine != null) StopCoroutine(coroutine);
coroutine = StartCoroutine(FireBreathing());
}
else if (isStrawbiliBoy == true)
{
if (coroutine != null) StopCoroutine(coroutine);
Debug.Log("Coroutine stop attempted");
coroutine = StartCoroutine(FireBreathing());
}
}
IEnumerator FireBreathing()
{
Debug.Log("Coroutine has started");
isStrawbiliBoy = true;
playerController.isNormalGerm = false;
playerController.moveSpeed = strawbiliMoveSpeed;
playerController.jumpForce = strawbiliJumpForce;
//animator.SetBool ("isStrawbiliBoy", true);
yield return new WaitForSeconds (strawbiliDuration);
//animator.SetBool ("isStrawbiliBoy", false);
playerController.isNormalGerm = true;
playerController.moveSpeed = playerController.normalMoveSpeed;
playerController.jumpForce = playerController.normalJumpForce;
isStrawbiliBoy = false;
Debug.Log("Coroutine has finished");
}
}
评论