满足条件时如何停止更新SetDownState() [英] How to stop an updating SetDownState() when a condition is met

查看:106
本文介绍了满足条件时如何停止更新SetDownState()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SetDownState()代码,因为有时我需要玩家同时按下两个按钮.问题在于,只要buttonSetDownState == true,它就会不断更新.

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.

这是按钮"脚本(从Assets Sample中获取):

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

这是代码的工作方式.根据情况,它会更改按钮的功能.满足条件后,它将运行WinningScript()Losing Script()

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

您在这里看不到它,但是Win条件将更改CrowdTypes

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.

推荐答案

这是否可能是您的解决方案:

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"

我不知道那里有什么奇怪的按钮库或您在使用什么按钮,但这是这里的一般算法解决方案:O

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

一个这样的子程序必须使用这么长的代码,它将无法像您所拥有的那样进行编译.另外-出于任何原因都不要使用"else-if".

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

这篇关于满足条件时如何停止更新SetDownState()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆