Making a game… More efficient to do this?
if (37 in keysDown) { //left arrow
if (sprite.state != 'left') sprite.state = 'left';
}
or this?
if (37 in keysDown) { //left arrow
sprite.state = 'left';
}
This is being called in my game’s update function (constantly, as fast as possible).
Sidenote:
here is my input key checking code.
//input
var keysDown = {};
window.addEventListener('keydown', function(e) {
keysDown[e.keyCode] = true;
});
window.addEventListener('keyup', function(e) {
delete keysDown[e.keyCode];
});
http://c2.com/cgi/wiki?PrematureOptimization
Those two ways share the same complexity, setting\changing a four chars variable won’t be the bottle-neck in your app.
The only things I’m concerned here is the readability of your code, if either way you want
sprite.stateto have the valueleftwhy do you need to check what was the previous value?(ohh, and it saves like 20 bits of bandwidth which is just like the performance gain here…)