Results 1 to 6 of 6

Thread: Move Multiple Directions

  1. #1

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Move Multiple Directions

    This has to do with direct input.

    Now, I notice in some amatuer source code, or if the game works fine with this method, some people just go
    (typing this in english, not code)

    if button=left

    else if button = right

    else if button = left

    ect

    Now this leaves little for play, and one cannot move on diangols...
    So A few if then statements would be nice, no else...the only else there should be on each if statement is to make sure the button is up.

    Now how would one go about getting multiple directions. I have chosen a method, and seen a few others.
    Here is my method, using c++ string

    Direction is a string
    The player pressed a button
    if button = left then
    Direction += Direction + "left"

    if button = right then
    Direction += Direction + "right"

    Then we the motion is carried out I just
    if (Direction.find("left",0) != string:npos)
    move the stuff

    Now I have also seen it done with ints, but an int representing a direcetion can get confusing to other programmers looking at yer stuff. I have seen individual key BOOLS for each key, which is consuming...Anyone have any nice methods they wish to explain.

    Note: in my game I use north, south, east, west, up, down.
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  2. #2
    Addicted Member
    Join Date
    Jun 2002
    Location
    Far.. far away! (Netherlands)
    Posts
    169
    I think using bools is less consuming compared to the string stuff you do at the moment. What I usually do is declare a boolean variable for every key that my game uses, then I create a function that checks for the states of all those keys, and stores true if the key is pressed, and false if it's not. Using this method, you can check the input anywhere in your program. Something like this:

    PHP Code:

    // global
    bool keyUp false;
    bool keyDown false;
    bool keyLeft false;
    bool keyRight false;

    // Get the states of the keys we're interested in
    void getInput()
    {
       
    keyUp GetKeyState(VK_UP) <  0;
       
    keyDown GetKeyState(VK_DOWN) <  0;
       
    keyLeft GetKeyState(VK_LEFT) <  0;
       
    keyRight GetKeyState(VK_RIGHT) <  0;
    }

    // Process the input
    void processInput()
    {
       
    int newX player.x;
       
    int newY player.y;

       
    // Check every key needed
       
    if(keyLeft){
          if(
    spotWalkable(newX-2,newY))
             
    newX-=2;
       }

       if(
    keyRight){
          if(
    spotWalkable(newX+2,newY))
             
    newX+=2;
       }
       if(
    keyUp){
          if(
    spotWalkable(newX,newY-2))
             
    newY-=2;
       }

       if(
    keyDown){
          if(
    spotWalkable(newX,newY+2))
             
    newY+=2;
       }
       
    // All done, set the new position
       
    player.newX;
       
    player.newY;

    Something like that.

  3. #3
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    As Shell said using bools is the fastest way. Strings are very very time consuming to use.

    It is not DInput but I wrote a tutorial I think on the technique in the FAQ....

    ØØ

  4. #4
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    The method I use is a Byte Array with 255 elements. Then I use the value 0 if the button isn't pressed, 1 if its just been pressed (occours for one frame just to show its inital press), 2 for while its being held down And finally -1 when it is released (similar to 1 apart from the other edge ).

    Not all the elements need to be processed each frame mind cos not all keys will be assigned during gameplay but the InGame Input process will just loop though the used keys and process them and ignore the other elements. But in the Options menu when you are redifining keys you will need to loop though all and keep checking for the value 1 so that you know which key the user is trying to redefine a key to .

    I'll post some code to go with this if you want it?


    P.S. For this method things like movement keys are done like: if (Keys[MoveUpKey] > 0). And for say typing words: if (Keys[SomeLetterKey] == 1).

    This also allows for special stuff like sounds to be activated and deactivated, for example:
    PHP Code:
    if (Keys[MoveUpKey] > 0)
    {
        if (
    Keys[MoveUpKey] == 1)
        {
            
    // Play Looping Sound...
        
    }
        
    // Do Movement Code...
    }
    else if (
    Keys[MoveUpKey] == -1)
    {
        
    // Stop Sound Playing...

    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  5. #5

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339
    I enjoy that method, but I do understand what you mean when you say using a ByteArray.

    Like I know what a ByteArray is, so that would mean then
    Keys() is a function, with a parameter
    Keys(DIK_KEY)
    which returns a value given its state? 0,1,2,-1??

    I don't get how you are getting those value's..I get everything else.
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  6. #6
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Posted by Halsafar
    I enjoy that method, but I do understand what you mean when you say using a ByteArray.

    Like I know what a ByteArray is, so that would mean then
    Keys() is a function, with a parameter
    Keys(DIK_KEY)
    which returns a value given its state? 0,1,2,-1??

    I don't get how you are getting those value's..I get everything else.
    Not a function but an array .

    So to define Keys I would do:
    PHP Code:
    char Keys[255]; 
    Or for VB:
    VB Code:
    1. Dim Keys(255) As Byte


    That should clear up how I am getting the values too? They are just the value of that element
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width