I would like to have an array dynamically generated by keypress, then have different JavaScript objects listening to changes in that array. For example, in games, pressing LEFT/RIGHT/UP/DOWN would store those 4 values in the array, which can be executed by different objects listening in to that array.
Is this a good approach? And if it is, how to go about setting up listeners to that array?
You’re probably not looking into using a simple Array, but your custom implementation that applies the Observer pattern.
The other (bad) alternative would be to poll changes to the array with a timer, but that doesn’t seem feasible for a game.Actually, since you’re writing a game, I would assume that you’ll have a game loop that runs on a timer. Each interested object could simply read from the array in each update cycle. There is probably no need to push real-time changes to these objects. Unless, of course, you can’t afford to lose any key strokes — in which case you could buffer the keys in a more complex structure than a simple Array.