当按钮调用多个功能时如何正确使用IEnumerator [英] How to Use IEnumerator Correctly when button calls multiple functions
问题描述
我正在制作一个纸牌游戏,在该游戏中,我试图在实例化每张纸牌之前进行(0.5f)延迟.我有实例化的代码和对象
I am making a card game in which I am trying to make a (0.5f) delay before each card is instantiated. I have my code which instantiates and object
public IEnumerator Name(int x,int y, int z)
{
}
在IEnum中,在带有实例化的所有代码之前,我都有一个yeild return new WaitForSeconds(0.5f)
.
In the IEnum i have a yeild return new WaitForSeconds(0.5f)
before all the code with the instantiation.
我分别使用
StartCoroutine(Name(...par...));
在我的游戏按钮上,我有4个事件,这些事件使用枚举生成卡,但没有延迟.
And on my play game button i have 4 events which use the enum to spawn the cards but there is no delay .
有没有一种方法可以使卡片一张一张出现.
Is there a way to make the cards appear 1 by one .
感谢您的支持.
推荐答案
当前正在调用StartCoroutine的内容是协程.
现在,您的代码看起来/行为如下:
Whatever it is that is currently calling StartCoroutine needs to be the coroutine.
Right now you have code that looks/behaves like this:
StartCoroutine(Name(...par...));
StartCoroutine(Name(...par...));
StartCoroutine(Name(...par...));
StartCoroutine(Name(...par...));
他们所有人都在生成卡片,而不是互相等待.您不需要这样做,因此您将需要对通话方式进行根本性的更改,以便获得以下行为:
And all of them are spawning a card and not waiting for each other. You don't want this, so you're going to need to make a fundamental change in how your calls are made so that you can get this behavior:
StartCoroutine(SomeMethod(...));
IEnumerator SomeMethod(...) {
yield return Name(...par...)
yield return Name(...par...)
yield return Name(...par...)
yield return Name(...par...)
}
这篇关于当按钮调用多个功能时如何正确使用IEnumerator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!