This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2007-03-21 17:21:17

bernatp
Scratcher
Registered: 2007-03-07
Posts: 49

Pressing 2 buttons at the same time

According to my experiences, a 'when ... key pressed' script is automatically stopped if any other key is pressed. It makes (a little) harder to make 2 player games in which both of the players should use the keyboard to control their sprites. (Or a "1-player example" is the classic "racing car motion", where moving forward and turning at the same time is a frequent case.)
However, I discovered that

When Green flag clicked
Forever if key ... pressed?

worked well. Maybe, it would be more elegant if 'when ... key pressed' worked just like this construction. Or, to be more precise, not just like. There is a slight difference between the "when-hat" and the "forever if" construction. The second one seems to be too sensitive, not allowing to run the script only once. So something hybrid would be adequate for the 'when ... key pressed' hats to work properly.
Are there any technical difficulties?

Offline

 

#2 2007-03-22 22:41:21

johnm
Scratcher
Registered: 2007-03-08
Posts: 100+

Re: Pressing 2 buttons at the same time

There is an important (but subtle) difference between the "when ... key pressed" hat and the "key ... pressed" sensor block.

The  "when ... key pressed" hat responds to incoming key events. Sometimes you want a sprite to do something exactly once when you press a key. The "when ... key pressed" hat is perfect for that. This hat is also a great way for beginners to start making interactive programs: they can use the arrow keys to move a sprite around, for example, with very few blocks and no explicit loops or conditionals.

Most computer keyboards support autorepeat, so if you hold down a key it generates a sequence of key presses (usually after a short initial pause). The key events re-trigger the "when ... key pressed" hat. But the keyboard will generate auto-repeat events for only one key at a time, so when one player is holding down a key it locks out the other player's "when ... key pressed" scripts.

In contrast, the "key ... pressed" sensor block returns true continuously while a key is being held down. It reports the key's *state* not key transition events. This is much better for two-player games because each player can continuously hold down a key without locking out the other player. The form that you describe, using  a "key ... pressed" inside a forever-if, is perfect for games. (Unfortunately, most keyboards only report key transitions for up to three keys at a time, however, so there are limits to this technique imposed by the keyboard hardware. It would not work well for a four-player game.)

As you've discovered, the two forms of keyboard input are useful for different tasks. I think eliminating either one would make certain things impossible (or at least very complicated).

  -- John

Last edited by johnm (2007-03-22 22:45:54)

Offline

 

Board footer