我在Unity JS中有以下代码。我需要的是当调用函数 Count1 时,函数 CountTime 停止并再次启动。与函数 Count2 和 Count3 类似。但是当调用函数 Count4 时,CountTime 应该停止并且不会再次恢复。
#pragma strict
import UnityEngine.UI;
private var textfield:Text;
private var textfield0:Text;
private var textfield1:Text;
private var textfield2:Text;
private var textfield3:Text;
var timer : float = 0;
function Start () {
textfield = GameObject.Find("TimerMain").GetComponent(Text);
textfield0 = GameObject.Find("Timer").GetComponent(Text);
textfield1 = GameObject.Find("Timer1").GetComponent(Text);
textfield2 = GameObject.Find("Timer2").GetComponent(Text);
textfield3 = GameObject.Find("Timer3").GetComponent(Text);
}
function Update(){
CountTime();
}
function CountTime()
{
timer += Time.deltaTime*10;
textfield.text = timer.ToString("0000");
}
function Count1(){
textfield0.text = textfield.text;
}
function Count2(){
textfield1.text = textfield.text;
}
function Count3(){
textfield2.text = textfield.text;
}
function Count4(){
textfield3.text = textfield.text;
}
请提供程序逻辑的更多详细信息。如果要在button1-2-3功能中设置定时器0,可以设置timer = 0f;
也许您可以添加一个标志您的 Count4 按钮事件。并在更新时检查它的值。我假设您要重置计时器计数1,计数2,计数3方法。
var timer : float = 0;
var count4IsPressed = false;
function Start () {
textfield = GameObject.Find("TimerMain").GetComponent(Text);
textfield0 = GameObject.Find("Timer").GetComponent(Text);
textfield1 = GameObject.Find("Timer1").GetComponent(Text);
textfield2 = GameObject.Find("Timer2").GetComponent(Text);
textfield3 = GameObject.Find("Timer3").GetComponent(Text);
}
function Update(){
if(!count4IsPressed){
CountTime();
}
}
function CountTime()
{
timer += Time.deltaTime*10;
textfield.text = timer.ToString("0000");
}
function Count1(){
textfield0.text = textfield.text;
timer = 0;
}
function Count2(){
textfield1.text = textfield.text;
}
function Count3(){
textfield2.text = textfield.text;
timer = 0;
}
function Count4(){
count4IsPressed = true;
textfield3.text = textfield.text;
}
您不能只做一个简单的选项,例如添加布尔检查以查看时间是应该打开还是关闭?然后,您可以在需要时暂停它,或者将其设置为false
并且永远不会重新启动。如果语法错误,请原谅我的语法,我只使用 c#。
var timer : float = 0;
var isCounting : boolean = true;
function Update(){
if (isCounting)
{
CountTime();
}
}
function Count1(){
isCounting = false;
// Do some stuff...
textfield0.text = textfield.text;
isCounting = false;
}
// Other methods/functions...
function Count4(){
isCounting = false;
textfield3.text = textfield.text;
}
或者,如果您需要时间返回0.0f
这些函数,只需将语句插入timer = 0.0f;
您想要重新开始时间的任何位置即可。