When anything is typed in, the letter shows up twice, instead of just once. Obviously, the loop instead of just adding the second character typed to the string is storing the info and adding the entire text back to itself, instead of just concatenating the new character.
create() { var enteredText = ''; this.input.keyboard.on('keydown', function (event) { enteredText += event.key;
console.log(enteredText);
}
The same problem happens with the example given (link above): Why, and how to just get the newly entered key to concatenate once to the original string?
Thanks but that doesn’t solve my problem. Why would mine be sending extra events? As I said it’s the same as the example online which also gives me the same error–double keys.
Adding console.log(event.type, event.key); gives me a single response “keydown, a”.
It runs the entire loop twice, but why would it do that? “Create” is not a loop.
This is my entire code:
class GameScene extends Phaser.Scene{
The reason it’s sending more than just once is a browser thing (afaik). best solution is checking on keyup, another solution is making a variable that’s true/false, having it set to false when ondown is triggered and making a tween that sets it to true after some time and making an IF statement that does that it only triggers when it’s false/true
Tried in Chrome and Firefox–same result.
Tried keyup, same problem.
Am I really the only one having this problem?
I can’t see a problem with my code at all.
When I press any key, the entire callback function runs twice. Why?