I have a weird issue with this code
Moonbeam.Input.KeyboardState = function Moonbeam_Input_KeyboardState() {
this._keys = new Array(2);
this._resetKeys();
}
Moonbeam.Input.KeyboardState.prototype = {
_resetKeys: function Moonbeam_Input_KeyboardState$_resetKeys() {
this._keys.clear();
},
_SetKeys: function Moonbeam_Input_KeyboardState$_SetKeys(keysPressed) {
this._resetKeys();
this._keys = keysPressed;
}
}
I call the _SetKeys function and pass in a value, on the line this._resetKeys(); KeysPressed contains the value, on the line this._keys = KeysPressed, after returning from the call to _resetKeys(), keysPressed contains no value.
this is not the behaviour I expect, but javascript is not my speciality.
Can anyone tell me is this something I am doing wrong?
Is this how javascript works – I dont see how you could do recursion if it is.
I have also tried adding the line var _keysPressed = keysPressed; before the call to this._resetkeys() in the _SetKeys function but that also loses its value.
How do I get my values to stay during a call to another function?
stu.
When you pass an array (or object parameter) to a function, it is not copied, the function receives a reference to that array. When you then do
this._keys = keysPressed;,_keyscontains that same reference. When you later dothis._keys.clear();, you’re modifying that array.I suspect what’s happening is that you’re calling
_setKeys()with the same array as the previous call. So when it callsthis._resetKeys(), it clears that array.You either need to create new arrays as you need them, perhaps by copying them when receiving them as parameters or returning them as values.