How to stop an updating SetDownState() when a condition is met

◇◆丶佛笑我妖孽 提交于 2019-12-11 09:55:43

问题


I am using the SetDownState() code because I need the player to push two buttons at the same time, sometimes. The problem is that any time the button is SetDownState == true it keeps updating.

What happens is while the button is pressed the Scripts below will run. But the button is still running. Thus, if the conditions change it ends up making the player Lose even though they Won.

This is the "Button" Script ( got this from the Assets Sample):

 namespace UnityStandardAssets.CrossPlatformInput

{
    public class YayButtonTouch : MonoBehaviour
    {

        public bool yayButtonTouch = false;

        public string Name;

        public void SetDownState()
        {

            yayButtonTouch = true;
        }


        public void SetUpState()
        {
            yayButtonTouch = false;
        }
    }
}

Here is how the code works. Depending on the condition it changes what the buttons do. Once the condition is met it runs the WinningScript() or Losing Script()

You can't see it here but the Win condition will change the CrowdTypes

void Update ()
{

    if (cm.crowdOptions == CrowdManager.CrowdTypes.None) {

        if (yt.yayButtonTouch == true)
        {
           if (bt.booButtonTouch == true)
           {
                iyt.WinningScript();
            }
    }

    if (cm.crowdOptions == CrowdManager.CrowdTypes.TeamA) {

        if (sm.teamScored == ScoreManager.teamsScore.TeamAScore) {

            if (yt.yayButtonTouch == true)
            {
                iyt.WinningScript();
            }

            if (bt.booButtonTouch == true)
            {
                iyt.LosingScript();
            }

        } else if (sm.teamScored != ScoreManager.teamsScore.TeamAScore) {

            if (yt.yayButtonTouch == true)
            {
                iyt.LosingScript();
            }

            if (bt.booButtonTouch == true)
            {
                iyt.WinningScript();
            }
    } 
}
if (cm.crowdOptions == CrowdManager.CrowdTypes.TeamB) {

     if (sm.teamScored == ScoreManager.teamsScore.TeamBScore) {

            if (yt.yayButtonTouch == true)
            {
                iyt.WinningScript();
            }

            if (bt.booButtonTouch == true)
            {
                iyt.LosingScript();
            }

     } else if (sm.teamScored != ScoreManager.teamsScore.TeamBScore) {

            if (yt.yayButtonTouch == true)
            {
                iyt.LosingScript();
            }

            if (bt.booButtonTouch == true)
            {
                iyt.WinningScript();
            }
     }
 }

I hope I explained this well enough.


回答1:


Is it possible this is your solution:

WHEN button A is touched DOWN,
  if button B is pressedDown,
    ... code for "both down"

So you need the event or whatever for "on button first pressed down"

I have no clue what strange button library or whatever it is you are using there, but that's the general algorithmic solution here :O

Another more general approach is this:

when any button at all goes either up or down, look at both buttons. in fact are BOTH buttons now "down" if so, .. code for "both down"

that will also work. Hope this helps


Note

one HAS TO USE subroutines for such long passages of code, it will not compile as you have it. Also - never use "else-if" for any reason.

You'll have to change your code like this:

void Update ()
    {
    if (cm.crowdOptions == CrowdManager.CrowdTypes.None)
        {
        SituationA();
        return;
        }

    if (cm.crowdOptions == CrowdManager.CrowdTypes.TeamA)
        {
        SituationB();
        return;
        }

    if (cm.crowdOptions == CrowdManager.CrowdTypes.TeamB)
        {
        SituationC();
        return;
        }

    // OR, USE A SWITCH STATEMENT FOR THOSE THREE. TRY BOTH WAYS!
    }

private void SituationA()
    {
    if (yt.yayButtonTouch == true)
       if (bt.booButtonTouch == true)
            iyt.WinningScript();
    }

private void SituationB()
    {
    if (sm.teamScored == ScoreManager.teamsScore.TeamAScore)
        {
        if (yt.yayButtonTouch == true) iyt.WinningScript();
        if (bt.booButtonTouch == true) iyt.LosingScript();
        }
    else
        {
        if (yt.yayButtonTouch == true) iyt.LosingScript();
        if (bt.booButtonTouch == true) iyt.WinningScript();
        } 
    }

private void SituationC()
    {
     if (sm.teamScored == ScoreManager.teamsScore.TeamBScore)
        {
        if (yt.yayButtonTouch == true)  iyt.WinningScript();
        if (bt.booButtonTouch == true) iyt.LosingScript();
        }
     else
        {
        if (yt.yayButtonTouch == true) iyt.LosingScript();
        if (bt.booButtonTouch == true) iyt.WinningScript();
         }
    }


来源:https://stackoverflow.com/questions/35217707/how-to-stop-an-updating-setdownstate-when-a-condition-is-met

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!