Page 1 of 1

Jumping Jack Flash

Posted: Sat Jul 04, 2015 9:49 am
by richmond62
One of the children on my Summer course was having 'problems' with animating a 'person'
to jump [ think "Super Mario"].

Well, I don't know what he is doing this weekend, but it took me all of 15 minutes to knock together a basic walker-cum-jumper:

https://www.dropbox.com/sh/tyt4o1md51bf ... QGg7a?dl=0 File: Walker.zip

HOWEVER, there is a slight snag insofar as that having been bashing the left arrowKey, when I bash the up arrowKey (to generate a jump),
the character jumps, but then continues to execute the code stored from my pressing the left or right key for far too long than is
necessary.

The same thing happens if I press the left key repeatedly, then the right key once.

SO
, what I need is some sort of INTERRUPT code so that when I press a new arrowKey all previous information fed by my key bashing gets lost.

Richmond.

Re: Jumping Jack Flash

Posted: Sun Jul 05, 2015 12:01 pm
by SparkOut
I haven't had a chance to look but would flushEvents("eventtype") be what you need here?

Re: Jumping Jack Flash

Posted: Sun Jul 05, 2015 3:40 pm
by richmond62
I tried flushEvents("autoKey") to no avail.

At present I am using pass arrowKey

which is not that satisfactory as the end-user has to keep
"pumping the keys" if they want events to repeat.

What I need is something that stops previous keyDown signals when the new
key is pressed, e.g.:

I press the LEFT arrowKey, and keep my finger on it, so the action should repeat itself, UNTIL
I press the RIGHT arrowKey, at which point all the stored LEFT arrowKey instructions should vanish.

BUT, what happens is that my RIGHT key instructions execute, followed by all the left-over LEFT ones,
which is, obviously, a load of sh*t.

Re: Jumping Jack Flash

Posted: Sun Jul 05, 2015 4:57 pm
by Newbie4
These notes in general from my website may offer at least one usable solution:

On two-player games using following code:

Code: Select all

for Player 1

    on movePlayer1
       // first check if the up-arrow key was pressed. If so, then move player 1 up
       if keysdown() contains 65362 then
          set the top of the button "player1" to the top of the button "player1" - 4
          if the top of the button "player1" < the top of the card "mycard" then
             set the top of the button "player1" to the top of the card "mycard"
          end if
       end if
       // next, check if the down-arrow key was pressed. If so, then move player 1 down
       if keysdown() contains 65364 then
          set the bottom of the button "player1" to the bottom of the button "player1" + 4
          if the bottom of the button "player1" > the bottom of the card "mycard" then
             set the bottom of the button "player1" to the bottom of the card "mycard"
          end if
       end if
       ... (etc for left and right)
    end movePlayer1

    on movePlayer2
       // first check if the up (letter "W") key was pressed. If so, then move player 2 up
       if keysdown() contains 119 then
          set the top of the button "player2" to the top of the button "player2" - 4
          if the top of the button "player2" < the top of the card "mycard" then
             set the top of the button "player2" to the top of the card "mycard"
          end if
       end if
       // next, check if the down (letter "S") key was pressed. If so, then move player 2 down
       if keysdown() contains 115 then
          set the bottom of the button "player2" to the bottom of the button "player2" + 4
          if the bottom of the button "player2" > the bottom of the card "mycard" then
             set the bottom of the button "player2" to the bottom of the card "mycard"
          end if
       end if
      ... (etc for left and right)
    end movePlayer2

1) If you find that 1 player overwhelms the other player (i.e. when one player holds down a key and keeps the other player from moving), add the following code to each player handler. It flushes the buffer after each time checking.

get flushevents "keyDown"

2) We use "contains" not "is" because it works better when there are 2 players and 2 different keys pressed

if keysdown() contains 115 then

3) To avoid repetition of code, a variable "direction" is set and used in other handlers. Only move in the direction the player is set, otherwise change "direction" to the new direction.

Hope this helps..