简体   繁体   English

如何在使用协程将当前乘数乘以 2 30 秒后保持当前乘数

[英]How to keep current multiplier after multiplying your current multiplier by two for 30 seconds with a coroutine

As the title says I would like to give my player a temporary 2x multiplier and then take it away after.正如标题所说,我想给我的播放器一个临时的 2 倍乘数,然后再把它拿走。 So far I have a button they click that sets their current multiplier x2 and then after 10 seconds take it away, the issue is if they buy an increase to their multiplier during that time it messes things up and I am not sure how to handle it.到目前为止,我有一个他们单击的按钮,用于设置他们当前的乘数 x2,然后在 10 秒后将其取走,问题是如果他们在那段时间购买增加的乘数,它会把事情搞砸,我不确定如何处理它. GameManager.The multiplier is set to 1 at the start of the game and can be added to by purchasing an "upgrade". GameManager.The multiplier 在游戏开始时设置为 1,可以通过购买“升级”来增加。

Edit: I have two scripts one that keeps track of how much Iron you have and What your current click multiplier is it also handles displaying both to the screen with text it's called GameManager.编辑:我有两个脚本,一个跟踪你有多少 Iron 和你当前的点击乘数是多少,它还处理将两者显示到屏幕上的文本,它称为 GameManager。

My game has 3 buttons one takes 10 Iron and gives the player plus 1 to their click multiplier the other 2 do the same thing, but requires more Iron and gives a higher bump to the click multiplier.我的游戏有 3 个按钮,一个需要 10 个 Iron,让玩家的点击倍数加 1,其他 2 个做同样的事情,但需要更多的 Iron 并为点击倍数提供更高的冲击。

I then added a 4th button that when pressed will give the player a 2x to their multiplier for 30 sec (ex current click multiplier is 5 after button press it is now 10 after 30 sec it returns to 5)然后我添加了第 4 个按钮,当按下该按钮时,玩家将获得 2 倍的乘数,持续 30 秒(之前的点击乘数在按下按钮后为 5,现在为 10,在 30 秒后返回到 5)

The issue is that if the player clicks the 2x button and then buys a 1x click multiplier upgrade they do not keep the one they purchased during the 2x button coroutine.问题是,如果玩家点击 2x 按钮,然后购买 1x 点击乘数升级,他们不会保留他们在 2x 按钮协程期间购买的那个。 (ex current click multiplier is 1 click the 2x button click multiplier is now 2 then buy a 1x upgrade so the click multiplier is now 3. After the 30 sec the multiplier should return to 2 but instead returns to 1 as if they never purchased the 1x upgrade) (当前点击乘数是 1 次点击,2x 按钮点击乘数现在是 2,然后购买 1x 升级,所以点击乘数现在是 3。30 秒后,乘数应该返回到 2,但返回到 1,就好像他们从未购买过1x 升级)

Hopefully, this is a bit better at explaining my issue.希望这能更好地解释我的问题。 I tried adding a temp var of my original multiplier and then using mathf.ABS to figure out the difference and then do something with that, but it's honestly starting to confuse the crap out of me so I'm asking here for advice lol My code for it so far is:我尝试添加原始乘法器的临时变量,然后使用 mathf.ABS 找出差异,然后用它做一些事情,但老实说,它开始让我感到困惑,所以我在这里征求意见大声笑我的代码到目前为止是:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class TwoX: MonoBehaviour
{
    [SerializeField] Button _TwoX;

void Start () {
        Button btn = TwoX.GetComponent<Button>();
        btn.onClick.AddListener(TaskOnClick);
    }

    void TaskOnClick(){
        StartCoroutine(test());
    }

   IEnumerator test()
    {
        GameManager.Multiplier = GameManager.Multiplier * 2;
        yield return new WaitForSeconds(10);
        GameManager.Multiplier = GameManager.Multiplier / 2;
    }

}

From your descriptions it sounds like GameManager.Multiplier is an int so the steps are indeed根据您的描述,听起来GameManager.Multiplier是一个int所以这些步骤确实是

  • initial value 1初始值1
  • * 2 => 2 * 2 => 2
  • + 1 => 3 + 1 => 3
  • / 2 => 1 / 2 => 1

since int division doesn't know about decimals (if it was a float the result would be 1.5f ).因为int除法不知道小数(如果它是一个float ,结果将是1.5f )。


The trick I would use would be to not use * and / at all but rather + and - :我会使用的技巧是根本不使用*/而是+-

private bool alreadyDoubled;

IEnumerator test()
{
    // avoid hat the routine is running multiple times if your player spams the button
    if(alreadyDoubled) yield break;

    // block other instances of this routine
    alreadyDoubled = true;

    // cache the original value!
    // the advantage of a Coroutine here is that you can simply keep
    // along a value within this Coroutines scope
    var addition = GameManager.Multiplier;

    // This basically equals doing * 2
    GameManager.Multiplier += addition; 

    yield return new WaitForSeconds(10);

    // This only removes the addition, regardless of what happened to the value in the meantime
    GameManager.Multiplier -= addition; 

    // allow the next instance of this routine to start
    alreaeyDoubled = false;
}

This time the steps will rather be这次的步骤将是

  • initial value 1初始值1
  • " * 2 " -> actually + 1 -> 2 " * 2 " -> 实际上+ 1 -> 2
  • + 1 => 3 + 1 => 3
  • " / 2 " -> actually - 1 -> 2 " / 2 " -> 实际上- 1 -> 2

Your main issue here is that you are blocking the function. A better solution is to do the multiplier unsetting in the Update function and checking how long it's been.您在这里的主要问题是您阻止了 function。更好的解决方案是在更新 function 中取消设置乘数并检查它已经持续了多长时间。

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class TwoX: MonoBehaviour
{
    [SerializeField] Button _TwoX;

    float upgradeTime = 0.0f;
    float delay = 10.0f
    bool isUpgraded = false;

    void Start ()
    {
        Button btn = TwoX.GetComponent<Button>();
        btn.onClick.AddListener(TaskOnClick);
    }

    void TaskOnClick()
    {
        if (!isUpgraded)
        {
            GameManager.Multiplier *= 2;
            upgradeTime = Time.time;
            isUpgraded = true;
        }
    }

    void Update()
    {
        if (Time.time >= upgradeTime + delay && isUpgraded)
        {
            GameManager.Multiplier /= 2;
            isUpgraded = false;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM