Multi threading?


#1

Is it possible to have multi-threading in an app? A student wants to be able to listen for multiple key presses at the same time, for example, one to move a character horizontally, and one to make it jump (even if pressed while the character is moving). code studio seems to be single-threaded, only able to listen to one key press at a time. Thanks! JR


#2

JavaScript is a single threaded language, so there’s no way to do true multi-threading, besides which App Lab isn’t optimized for keyboard input. You can get the kind of behavior you want, however, by tracking both the “keydown” and “keyup” events instead of the “kepress” event - here’s a quick and dirty example of how this can track multiple keys at the same time https://studio.code.org/projects/applab/HYysUq5WHVTLDka_prkW5g/view - in this program I’m using the properties of design elements to keep track of which keys are down, but you’d probably want to do that with variables for a game.

Also worth noting, Game Lab is a sibling tool to App Lab that is optimized for this kind of programming.


#3

Thanks. I ended up coding something similar that kicks off a timer loop when one of the keys is pressed, while still “listening” and responding to the other keys (in between timer ticks, but it feels almost multi-threaded). JR