3

I'm new to programming, and I was wondering how you are able to handle consecutive, timed key presses in order for the character to do something different. For example, in games like 'Super Smash Bros.' you press one button for a character to punch, and if you press it again, they will do their second punch; pressing it once more will result in a kick. How am I able to program this in Scratch 3.0 (preferrably, but an explanation in another programming language might help)?

By the way, if this helps, I am using a 'state' variable in order to handle other animations such as running; I want the character to be able to animate when the timed key presses occur.

  • I think that you need to use a variable to count how many times the key was pressed. When it's pressed, check if timer is less than a specified value and increment the variable, otherwise reset it. Then reset the timer. – Maximouse Feb 27 '20 at 08:16
  • 1
    If I find the time I will write an answer on how to use [finite automata](https://en.wikipedia.org/wiki/Deterministic_finite_automaton) (*also known as finite state machines*) to track key presses. Every time you press a key you'd capture a timestamp and move to a new state if a state transition exists for the pressed key, and only if the last keypress was recent enough. If you move to a state that is "final", the combo is executed and the state machine is reset. – Romen Feb 27 '20 at 21:03
  • Thanks for the explanation MaxiMouse, but could you please write an example code? –  Feb 27 '20 at 21:13
  • Thanks Romen, I'd be grateful if you could! ;) –  Feb 27 '20 at 21:14

3 Answers3

0

Makey makey (in the extensions) does something similar to that, but if you wanted to make your own from scratch (no pun intended) you could do something like this example that I made: https://scratch.mit.edu/projects/382244376/

Isla
  • 85
  • 9
0

As stated earlier, makey makey is the only option... enter image description here

I know, they only have a few selected options

BUT!

try this: You usually use

when (up right down left v) pressed:
// Do something

enter image description here

Why don't you use

when (join(up up right right left left up down)()) pressed:
// Do Something

enter image description here

Yes, its the join()() block. Fill up the first blank with the combos you want, separated by a space, and leave the 2nd one empty. Drag it into the selectable part of the Makey Makey hat block, and there you go!

0

Create a variable named 'buttons pressed' and 'punch keybind' then do this script:

Script

when green flag clicked
set punch keybind to (A)
forever
  if <key (punch keybind) pressed?> then
    set buttons pressed to (join (buttons pressed) (punch keybind))
    if <(buttons pressed) contains (join (punch keyboard) (join (punch keyboard) (punch keyboard)))?> then
      COMBO ACTION
      set buttons pressed to ()
    end
    wait until <not <key (punch keybind) pressed?>>
  end
end

COMBO ACTION will be replaced with the animation and the kick script