Leave/End the IEnumerator the right way.
Hi there,
i have an IEnumerator which has to do a ColorCheck.
IEnumerator StartColorCheck()
{
if(checkingColors)
{
//Debug.Log ("can't StartColorCkeck");
yield break;
}
checkingColors = true;
yield return new WaitForSeconds (0.5f);
for (int i = 0; i < allcards.Length; i++)
{
CC = allcards[i].GetComponent<ColorCheck>();
CC.CheckColors();
CardMatches += CC.matches;
}
checkingColors = false;
}
The Problem is, it seems to stack up, until all calls are gone through it. I call it from a
void OnMouseUp()
but when i click to fast for example, all clicks seem to stack until all ran through it. Is there a way to avoid that? Because CardMatches returns a wrong value while it's repeating all over and over.
Thanks in advance.
Answer by Statement · Nov 03, 2015 at 08:23 PM
I don't really understand your problem. Doesn't the above example work?
Note you could also replace that with a normal function and use Invoke.
void OnMouse()
{
SingleInvoke("ColorCheck", 0.5f); // Call ColorCheck after half a second.
}
void SingleInvoke(string method, float time)
{
CancelInvoke(method); // If method is pending, cancel it.
Invoke(method, time);
}
void ColorCheck()
{
for (int i = 0; i < allcards.Length; i++)
{
CC = allcards[i].GetComponent<ColorCheck>();
CC.CheckColors();
CardMatches += CC.matches;
}
}
Basicly all works yes, but if i click to fast it mess up the result of Card$$anonymous$$atches since CheckColors seem to count to many.
$$anonymous$$aybe it's be cause my On$$anonymous$$ouse()
void On$$anonymous$$ouseUp()
{
if (isdragged && hitPlayfield)
{
transform.position = newPosition;
isdragged = false;
//play sound
CardAudio.clip = SwapClip;
CardAudio.Play();
//particles
Instantiate(ParticleFX,newPosition + Vector3.up * 0.2f,Quaternion.identity);
//do colorcheck in gamemanager
G$$anonymous$$S.InitColorCheck();
}
else if (isdragged && hitCard)
{
transform.position = newPosition;
DT.transform.position = oldPosition;
isdragged = false;
//play sound
CardAudio.clip = SwapClip;
CardAudio.Play();
//particles
Instantiate(ParticleFX,newPosition + Vector3.up * 0.2f,Quaternion.identity);
//do colorcheck in gamemanager
G$$anonymous$$S.InitColorCheck();
}
else//just rotated
{
transform.position = oldPosition;
isdragged = false;
//play sound
CardAudio.clip = RotateClip;
CardAudio.Play();
//Instantiate(ParticleFX,oldPosition + Vector3.up * 0.2f,Quaternion.identity);
//do colorcheck in gamemanager
//G$$anonymous$$S.InitColorCheck();
//do it in rotatescript
if(!RS.isRotating)
G$$anonymous$$S.InitColorCheck ();
Debug.Log ("rotation corouine fired");
}
}
I'll try your way with invoke.
It shouldn't make a difference. But are you setting Card$$anonymous$$atches to zero? It seems that you just keep adding and adding matches. I don't know if that is correct behaviour? If you had 3 matches, drag something, and you still have 3 matches, your variable will now be 3+3.
Yes is reset the Card$$anonymous$$atches on$$anonymous$$ouseDown()
and call this function from there:
public void ResetCard$$anonymous$$atches()
{
//if (!gameWon)
{
Card$$anonymous$$atches = 0;
for (int i = 0; i < allcards.Length; i++)
{
allcards [i].GetComponent<ColorCheck> ().matches = 0;
//deactivate all highlights
allcards [i].GetComponent<ColorCheck> ().Highlight1.SetActive (false);
allcards [i].GetComponent<ColorCheck> ().Highlight2.SetActive (false);
}
}
}
Your answer
Follow this Question
Related Questions
StartCoroutine still runs 1 more second after using yield break 1 Answer
IEnumerator when button is pressed 0 Answers
Flash Image once 1 Answer
void don't Run which tagged "Cmd" in IEnumrator :( 0 Answers
Do Slowmotion effect for x seconds 1 Answer