Outerra forum
Anteworld - Outerra Game => Tech demo, support, updates => Topic started by: zzz on September 19, 2015, 07:37:47 pm
-
Having a bit of trouble with extended keys. I can only get them to toggle. Has anyone got them so that a variable = 1 on the keypress and resets to 0 when the key is released?
For example, converting this deprecated code:
//handle extra actions
function action(k,v,dt)
{
switch(k){
// Pitch Down command
case AAuxb1:
if (v == 1) {
movePitchDown = 1;
}
if (v == 0) {
movePitchDown = 0;
}
break;
}
I tried
this.register_event("car/controls/power", function() {
movePitchDown = 1;
if (movePitchDown == 1) {
movePitchDown = 0;
}
}
});
and
this.register_switch("car/controls/power", function(v) {
if (v==1) {
movePitchDown = 1;
} else {
movePitchDown = 0;
}
});
and several variations.
-
register_event only invokes on key press.
register_switch is a toggle.
You should use register_axis like this:
this.register_axis("car/controls/power", {minval:0, center:1000, vel:1000, acc:1000}, function(v){ movePitchDown = v })
Vel defines the speed at which the value changes during key press, you could also use Number.POSITIVE_INFINITY there instead of 1000 to guarantee instant switches, but in reality nothing is instant so you should have there a number that says how fast it switches. A value of 1000 means that it changes from 0 to 1 in 1/1000 sec.
Center tells how fast the value goes back to 0 upon button release. If it's zero, the value stays where it was and you need a second key binding with negative mode that can push it back. Also, if you only have one key bound to such an action, it automatically becomes a toggle.