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

#1 2010-04-29 17:45:39

jdabrows
Scratcher
Registered: 2010-04-29
Posts: 4

Multiple Key Presses

Hello,

I am working on my first Scratch project (a simple game) but I cannot find a way to accommodate multiple key presses. For example: I would like the '->' key to make Sprite1 move 10 steps to the right, and the 'd' key to make Sprite2 move 10 steps to the right, but pressing '->' and 'd' at the same time will cause Sprite3 to move 10 steps to the right. Is this kind of behavior even possible in Scratch?

Any help whatsoever would be greatly appreciated - thanks so much!  smile

Offline

 

#2 2010-05-01 07:49:09

juststickman
Scratcher
Registered: 2009-05-31
Posts: 1000+

Re: Multiple Key Presses

Well... Try making two variables, I'll call them varA and varB for this.

On the first sprite, add this:
if <key [ right arrow ] pressed> {
set varA to 1
}
else {
set varA to 0
}
if <key [ d ] pressed> {
set varB to 1
}
else {
set varB to 0
}
if <varA = 1> and <varB = 0> {
move 10 steps
}

And then on the second sprite:

if <varA = 0> and varB = 1> {
move 10 steps
}

And then on the third
if <varA = 1> and <varB = 1> {
move 10 steps
}


http://is.gd/iBQi2 Add grob to your sig and help with world dominiation!http://is.gd/iBQ9Q                                                             Hey guys, we're seriously naming our team bob?

Offline

 

#3 2010-05-01 21:16:28

jdabrows
Scratcher
Registered: 2010-04-29
Posts: 4

Re: Multiple Key Presses

I tried your solution but my second variable (varB in your example) only stays at 1 for a fraction of a second, and then returns to 0 immediately no matter what I do. How do I make a variable stay at 1 indefinitely?

Thanks again for your help.

Offline

 

#4 2010-05-01 21:32:03

jdabrows
Scratcher
Registered: 2010-04-29
Posts: 4

Re: Multiple Key Presses

Never mind, I figured out the problem! There was another sprite which was trying to modify varB at the same time and that was confusing things.

Offline

 

#5 2010-05-01 22:44:09

CharlieAZ
Scratcher
Registered: 2008-12-24
Posts: 100+

Re: Multiple Key Presses

jdabrows wrote:

Never mind, I figured out the problem! There was another sprite which was trying to modify varB at the same time and that was confusing things.

so did you fix the button changing thingy?

Offline

 

#6 2010-05-03 08:53:52

jdabrows
Scratcher
Registered: 2010-04-29
Posts: 4

Re: Multiple Key Presses

CharlieAZ wrote:

so did you fix the button changing thingy?

"Button changing thingy?" Do you mean pressing two keys at once to get Sprite3 to move? Then yes I did. I used variables like juststickman suggested and added a wait statement immediately after "Set var___ to 1" so there would be a little bit of lag time after each key press to make "catching" them easier.

Offline

 

Board footer