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

#1 2008-07-31 10:53:18

SonicPopsDad
Scratcher
Registered: 2008-07-30
Posts: 41

Handling keyboard and mouse input... too slow and too fast!?!

One small problem I've encountered in my early days of Scratch programming is getting 'clean' inputs...
If I use the keys for movement with sensor 'key pressed' there's the standard Windows pause before auto-repeat kicks in.... so I get  * one movement * short pause * continuous movement *

Meanwhile, when I'm taking mouse button input, I'm getting the reverse problem - when I just want to take the first mouse click (using  sensor 'mouse down'), I often get 2 or 3 hits.   In my Connectorius project this leaves me with the side effect that some shots use, for example, 5 power units, but other shots use 10 or 15, even though I feel like I've clicked as 'lightly' as possible.

I've not yet worked out a way around these challenges... is there a standard way around them?  Or do I just need to accept them and try and mitigate them in the gameplay design?

Any ideas gratefully received  smile

Last edited by SonicPopsDad (2008-07-31 10:56:01)

Offline

 

#2 2008-07-31 11:01:43

coolstuff
Community Moderator
Registered: 2008-03-06
Posts: 1000+

Re: Handling keyboard and mouse input... too slow and too fast!?!

A way around the pause before repeat is to do
If <key (whatever key) pressed>
do action
========

and a way around the mouse click is to do
if < <mouse down> and <touching mouse-pointer> >
do action
wait until <not <mouse down> >

Or you could do (for the mouse), use the when (sprite) clicked hat, but there is a delay and you can't put it into wait untils or anything.

Hope this helped!

Last edited by coolstuff (2008-07-31 11:07:04)

Offline

 

#3 2008-07-31 11:25:35

SonicPopsDad
Scratcher
Registered: 2008-07-30
Posts: 41

Re: Handling keyboard and mouse input... too slow and too fast!?!

Coolstuff,

I'm not sure I get the key example (which is a bit embarrassing as it looks so simple!), because it looks the same as the movement routine I wrote this morning (for an Asteroids game), which looked like this (iirc)...

Spaceship sprite
forever
   if key Left Arrow pressed
        rotate ...

   if key Right Arrow pressed
        rotate ...
(repeat)

However I definitely *do* get the mouse one, that's great and should cure my problem!  thankyou!

Offline

 

#4 2008-07-31 11:40:03

coolstuff
Community Moderator
Registered: 2008-03-06
Posts: 1000+

Re: Handling keyboard and mouse input... too slow and too fast!?!

SonicPopsDad wrote:

Coolstuff,

I'm not sure I get the key example (which is a bit embarrassing as it looks so simple!), because it looks the same as the movement routine I wrote this morning (for an Asteroids game), which looked like this (iirc)...

Spaceship sprite
forever
   if key Left Arrow pressed
        rotate ...

   if key Right Arrow pressed
        rotate ...
(repeat)

However I definitely *do* get the mouse one, that's great and should cure my problem!  thankyou!

Hm... the repeat-after-wait thinghymabobber only happens really when I use the when ___ key pressed hat (script header). I don't know what could've happened there.

Offline

 

#5 2008-07-31 15:37:32

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Handling keyboard and mouse input... too slow and too fast!?!

This project here shows a common approach to handling the key press lag issue:

http://scratch.mit.edu/projects/Paddle2SeeFixIt/168376


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#6 2008-08-01 09:26:14

SonicPopsDad
Scratcher
Registered: 2008-07-30
Posts: 41

Re: Handling keyboard and mouse input... too slow and too fast!?!

You know, I should really answer these forum posts when I'm at home, and I can try to replicate my original problems ...  wink    I can't remember what I did that made the key board input work poorly, but Paddle2See, the project you posted works perfectly (though it's what I htought I'd originally done!) ...  I copied the code into my Defender game, and it's great  smile   (I''ve now got mouse *and* keyboard input working for it, which will suit old-school gamers like me!)

edit: oh yeah, Coolstuff mentioned it - must've used the 'when key pressed' block rather than the sensor module

Last edited by SonicPopsDad (2008-08-01 09:27:45)

Offline

 

#7 2008-08-01 11:41:14

newareagle
Scratcher
Registered: 2008-06-10
Posts: 100+

Re: Handling keyboard and mouse input... too slow and too fast!?!

Or if you are broadcasting something from using the mouse, you can always do broadcast _____ and wait. This means that the loop pauses until all of the actions are completed. Also if you want something to hide when you click it remember to put hide in before broadcasting and changes of variables.


My Guitar Hero Game.
8/9 Projects on the Front Page at Once!

Offline

 

#8 2008-08-01 22:48:40

JSO
Community Moderator
Registered: 2007-06-23
Posts: 1000+

Re: Handling keyboard and mouse input... too slow and too fast!?!

I think this is the "problem":

[blocks]
<when[  ]key pressed>
[/blocks]
This is an event: it depends on the key repeat in windows. Try holding a kay in wordpad and see what happens...



[blocks]
<key[  ]pressed?>
<mouse down?>
[/blocks]

These variables are actually some kind of boolean variables: true/false. When you put in a loop it might loop  or more times trough it because the condition keeps true. Adding a 0.1 wait might solve the problem.


http://oi48.tinypic.com/2v1q0e9.jpg

Offline

 

#9 2008-08-01 22:52:22

coolstuff
Community Moderator
Registered: 2008-03-06
Posts: 1000+

Re: Handling keyboard and mouse input... too slow and too fast!?!

JSO wrote:

Adding a 0.1 wait might solve the problem.

Another way (which I almost always use) is a [wait until <not <mouse down> >] block. Then, even if the user holds the mouse down for a bit longer (my mouse sometimes thinks I'm pressing for longer than I am), it still has the same effect.

Offline

 

#10 2008-08-02 16:45:45

SonicPopsDad
Scratcher
Registered: 2008-07-30
Posts: 41

Re: Handling keyboard and mouse input... too slow and too fast!?!

cheers guys, I've got a new project http://scratch.mit.edu/projects/SonicPopsDad/231508 which makes use of the mouse wait.  I've got another project on the go which actually uses the single-key input effect as a positive thing, so it's good we've got access to alternatives!

newareagle: thanks for pointing out 'broadcast and wait' ... I hadn't noticed that, I've used 'waits' and timers to get around any issues I've had - that way will be nicer  smile

Offline

 

Board footer