问题
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