You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何重置PowerUP持续时间计时器?已尝试StopCoroutine无效求助

解决Power-Up计时重置失效问题

你现在的问题出在:每次调用StopCoroutine(TripleShotPowerDownRoutine())时,都会生成一个全新的IEnumerator实例,这个实例和之前启动的协程完全不是同一个对象,所以StopCoroutine根本找不到要停止的那个正在运行的协程。旧的协程会继续走完剩下的时间,到点就关闭Power-Up,看起来就像是只续了剩余的1秒,而不是重置成5秒。

解决方法是给每个计时型Power-Up保存协程的引用,用这个引用来精准停止和重启协程:

// 新增两个变量存储协程实例
private Coroutine _tripleShotCoroutine;
private Coroutine _speedBoostCoroutine;

public void TripleShotActive()
{        
    _canTripleShot = true;
    // 停止正在运行的旧协程
    if (_tripleShotCoroutine != null)
    {
        StopCoroutine(_tripleShotCoroutine);
    }
    // 启动新协程并保存引用
    _tripleShotCoroutine = StartCoroutine(TripleShotPowerDownRoutine());
}

public void SpeedBoostPowerupActive()
{        
    _speedBoostActive = true;
    if (_speedBoostCoroutine != null)
    {
        StopCoroutine(_speedBoostCoroutine);
    }
    _speedBoostCoroutine = StartCoroutine(SpeedBoostPowerDownRoutine());
}

public void ShieldsActive()
{
    _isShieldsActive = true;
    _shieldVisualizer.SetActive(true);
}

IEnumerator TripleShotPowerDownRoutine()
{
    yield return new WaitForSeconds(5.0f);
    _canTripleShot = false;
    // 协程结束后清空引用,避免后续判断出错
    _tripleShotCoroutine = null;
}

IEnumerator SpeedBoostPowerDownRoutine()
{
    yield return new WaitForSeconds(5.0f);
    _speedBoostActive = false;
    _speedBoostCoroutine = null;
}

修改后,每次拾取同类型Power-Up时,都会先停止之前正在计时的协程,再重新启动一个新的5秒计时协程,完全重置计时器。协程结束后清空引用,确保下次判断时不会出现错误。

内容的提问来源于stack exchange,提问作者Annie Alves

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 20:40:28