UPDATE:第二个想法,我可以立即重置switch语句之外的值。为此,我需要检查案例是否已更改为NEXT案例值。例如,如果我的值目前在案例1上(游戏进入1秒),我该如何检查计时器何时进入NEXT值(在本例中为30)。您认为这是一种简单的方法吗?
我正在创建一个具有正在运行的计时器 current.getTime()的游戏。在游戏中的不同时间戳触发事件。我在switch语句中设置了这些事件的所有变量。
大多数变量随每种情况而变化,但有两种变量在每个语句中都会重置为默认值:
1)current.GetTime()
2)setTrapSprung(false);
如何重置这些值而不必将其放在每个案例中?最后,我将有100多个案例,因此要重复200多行代码。
注意:我无法在switch语句之外设置这些值(例如,在设置案例之后),因为在事件之间的那段时间内,用户实际上可能会弹出陷阱,从而将值设置为true。
var eventsHallOne = function () {
switch (current.getTime()) {
// 1st second in the game
case 1:
room.hallOne.setCurUrl(camHallOne.c21);
room.hallOne.setNextUrl(null);
room.hallOne.setTrapUrl(camHallOne.c130422);
room.hallOne.setCatchTime(3);
/* How do I reset these two variables for EACH statement, without having to set them within
* each statement? Each time an event occurs, they will have their value reset to what you see below. */
room.hallOne.setTime(current.getTime());
room.hallOne.setTrapSprung(false);
break;
// 30 seconds into the game
case 30:
room.hallOne.setCurUrl(camHallOne.c6543);
room.hallOne.setNextUrl(null);
room.hallOne.setTrapUrl(camHallOne.c123522);
room.hallOne.setCatchTime(6);
room.hallOne.setTime(current.getTime());
room.hallOne.setTrapSprung(false);
break;
// 45 seconds into the game
case 45:
room.hallOne.setCurUrl(camHallOne.c53);
room.hallOne.setNextUrl(null);
room.hallOne.setTrapUrl(camHallOne.c130);
room.hallOne.setCatchTime(15);
room.hallOne.setTime(current.getTime());
room.hallOne.setTrapSprung(false);
break;
}
/* Could reset the values here if I could detect that the case has changed. Unsure of how to do so, however. */
};
答案 0 :(得分:3)
我不能在switch语句之外设置这些值(例如,在设置case之后),因为在事件之间的那段时间内,用户实际上可能会弹出陷阱,从而将值设置为true。
错误。在处理任何其他事件之前,您的eventsHallOne
函数将运行完成 - javascript是单线程的。请注意,即使它不是,在case
块中的前一个语句之后或在switch
块之后的前一个语句之后执行它也没有区别。
你可以而且应该在你的功能结束时简单地移动它。
如果您希望default
案例不执行任何操作,只需在其中放置return;
,以便不再执行switch
块之后的语句。
答案 1 :(得分:1)
它不仅仅是最后两行重复。您正在重复六行,只有您传递的值发生变化。
将所有六行放在一个函数中,并在switch语句中调用该函数。
function buildState(curUrl, nextUrl, trapUrl, catchTime) {
room.hallOne.setCurUrl(curUrl);
room.hallOne.setNextUrl(nextUrl);
room.hallOne.setTrapUrl(trapUrl);
room.hallOne.setCatchTime(catchTime);
room.hallOne.setTime(current.getTime());
room.hallOne.setTrapSprung(false);
}
....
case 1:
buildState(camHallOne.c21, null, camHallOne.c130422, 3);
break;
....
更好的是,创建一个查找数组并使用它。
var timeStates = [
{"id": 1, state: {curUrl: camHallOne.c21,
nextUrl: null,
trapUrl: camHallOne.c130422,
catchTime: 15 }
]);
然后你可以向数组添加新的状态,你的case语句(可能会变成一个循环)永远不需要改变。
按照他们的方式,这个问题非常适合StackExchange Code Review。
答案 2 :(得分:1)
位扭曲,但将开关更改为具有以下功能的闭包对象:
var eventsHallOne = (function(){
var events = {
"1": function hallOne1Sec() {
room.hallOne.setCurUrl( camHallOne.c21 );
room.hallOne.setNextUrl( null );
room.hallOne.setTrapUrl( camHallOne.c130422 );
room.hallOne.setCatchTime( 3 );
},
"30": function hallOne30Sec() {
room.hallOne.setCurUrl( camHallOne.c6543 );
room.hallOne.setNextUrl( null );
room.hallOne.setTrapUrl( camHallOne.c123522 );
room.hallOne.setCatchTime( 6 );
},
"45": function hallOne45Sec() {
room.hallOne.setCurUrl( camHallOne.c53 );
room.hallOne.setNextUrl( null );
room.hallOne.setTrapUrl( camHallOne.c130 );
room.hallOne.setCatchTime( 15 );
}
}
return function eventsHallOne() {
var time = current.getTime().toString();
var foundEvent = events[time];
if ( foundEvent !== undefined ) {
foundEvent();
room.hallOne.setTime( current.getTime() );
room.hallOne.setTrapSprung( false );
}
};
})();
答案 3 :(得分:0)
这有点黑客,但它可能对你有所帮助:
如果在没有输入任何情况的情况下添加被触发的default
分支来反转switch的逻辑怎么样,在那里设置一些temporaty变量,在切换之后,如果没有输入默认情况,重置那些变量?< / p>
var eventsHallOne = function () {
var importantTimeAchieved = true;
switch (current.getTime()) {
// 1st second in the game
case 1:
room.hallOne.setCurUrl(camHallOne.c21);
room.hallOne.setNextUrl(null);
room.hallOne.setTrapUrl(camHallOne.c130422);
room.hallOne.setCatchTime(3);
break;
// 30 seconds into the game
case 30:
room.hallOne.setCurUrl(camHallOne.c6543);
room.hallOne.setNextUrl(null);
room.hallOne.setTrapUrl(camHallOne.c123522);
room.hallOne.setCatchTime(6);
break;
// 45 seconds into the game
case 45:
room.hallOne.setCurUrl(camHallOne.c53);
room.hallOne.setNextUrl(null);
room.hallOne.setTrapUrl(camHallOne.c130);
room.hallOne.setCatchTime(15);
break;
// none of important times
default:
importantTimeAchieved = false;
}
if (importantTimeAchieved) {
room.hallOne.setTime(current.getTime());
room.hallOne.setTrapSprung(false);
}
};