none
Constant Mo RRS feed

  • Question

  • I'm programming a game, and I want my player to continue moving in whatever direction that has been indictated by the user with the keys, even when the key is not being held down anymore. Right now, my code moves the "player" while the key is held down. but not after they release it.

    Here is my current code:

    player= Shapes.AddEllipse(15,40)
    x=0
    y=0
    GraphicsWindow.TextInput= MoveCycle
    Sub MoveCycle
      If (GraphicsWindow.LastKey= "D") Then
        x=x+5
        Shapes.Move(player,x,y)
      ElseIf (GraphicsWindow.LastKey= "A") Then
        x=x-5
        Shapes.Move(player,x,y)
      ElseIf (GraphicsWindow.LastKey= "S") Then
        y=y+5
        Shapes.Move(player,x,y)
      ElseIf (GraphicsWindow.LastKey= "W") Then
        y=y-5
        Shapes.Move(player,x,y)
      EndIf
    EndSub

    Any help would be appreciated!
    Tuesday, November 20, 2012 7:54 PM

Answers

  • Really what you want the user to change is the player velocity, not its position.  The position will be updated from the velocity regardless of the current key state.

    player= Shapes.AddEllipse(15,40)
    x=0
    y=0
    u = 0 'X direction velocity
    v = 0 'Y direction velocity
    GraphicsWindow.TextInput= MoveCycle
    
    While ("True")
      'Update position from velcoties
      x = x+u
      y = y+v
      'Do the player position update
      Shapes.Move(player,x,y)
      Program.Delay(20) 'Delay to keep it smooth
    EndWhile
    
    Sub MoveCycle
      If (GraphicsWindow.LastKey= "D") Then
        u=u+1
      ElseIf (GraphicsWindow.LastKey= "A") Then
        u=u-1
      ElseIf (GraphicsWindow.LastKey= "S") Then
        v=v+1
      ElseIf (GraphicsWindow.LastKey= "W") Then
        v=v-1
      EndIf
    EndSub

    • Proposed as answer by Math Man Tuesday, November 20, 2012 9:06 PM
    • Marked as answer by pleasehelpmeicantcode Wednesday, November 21, 2012 5:25 PM
    Tuesday, November 20, 2012 8:02 PM
    Moderator

All replies

  • Really what you want the user to change is the player velocity, not its position.  The position will be updated from the velocity regardless of the current key state.

    player= Shapes.AddEllipse(15,40)
    x=0
    y=0
    u = 0 'X direction velocity
    v = 0 'Y direction velocity
    GraphicsWindow.TextInput= MoveCycle
    
    While ("True")
      'Update position from velcoties
      x = x+u
      y = y+v
      'Do the player position update
      Shapes.Move(player,x,y)
      Program.Delay(20) 'Delay to keep it smooth
    EndWhile
    
    Sub MoveCycle
      If (GraphicsWindow.LastKey= "D") Then
        u=u+1
      ElseIf (GraphicsWindow.LastKey= "A") Then
        u=u-1
      ElseIf (GraphicsWindow.LastKey= "S") Then
        v=v+1
      ElseIf (GraphicsWindow.LastKey= "W") Then
        v=v-1
      EndIf
    EndSub

    • Proposed as answer by Math Man Tuesday, November 20, 2012 9:06 PM
    • Marked as answer by pleasehelpmeicantcode Wednesday, November 21, 2012 5:25 PM
    Tuesday, November 20, 2012 8:02 PM
    Moderator
  • You can also take a look in this Nyan Cat Game (WXR400-16).

    It keeps on moving at the same direction until changed by the player.

    At the end of the source code you can see a subroutine called Keyboard.

    It uses 2 variables: delX & delY.

    delX controls the horizontal movement, and delY, the vertical one.

    So, when delX is negative, Nyan moves to left, when positive to right; and when it's zero, it means Nyan is moving vertically, accordingly to delY now!

    Both delX & delY are used within main loop to change Nyan's position coordinate pair (x,y):

    x= x + delX
    y= y + delY

    Pretty ingeniously simple, huh?  :D


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Tuesday, November 20, 2012 8:31 PM
    Answerer