Thread: Virtual Arrow Problem...

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    Virtual Arrow Problem...

    (Borland C++ Free command line compiler, WinXP)
    Ok, I made my engine its all working pritty well. But I have one problem. When I implimented jumping, I expected it to detect if the up and left arrows were down (Because of how its built). But I guess it only detects one or the other. So, when jumping, you will only go up or left even when holding the right arrow and up and with the left to.

    (Include windows.h)

    So, I tried:

    Code:
    int IsArrowDown() {
     if(GetAsyncKeyState(VK_UP)&SHRT_MAX)
      return Up;
     if(GetAsyncKeyState(VK_DOWN)&SHRT_MAX)
      return Down;
     if(GetAsyncKeyState(VK_LEFT)&SHRT_MAX)
      return Left;
     if(GetAsyncKeyState(VK_RIGHT)&SHRT_MAX)
      return Right;
     if(GetAsyncKeyState(VK_ESCAPE)&SHRT_MAX)
      return Esc;
    
     return 0;
    }
    Code:
    int Check(int x) {
     if(x==Right) {
      MoveRight();
      return 1;
      }
    
     if(x==Left) {
      MoveLeft();
      return 2;
     }
    
     if(x==Up && x==Right) {
      MoveRight();
      return 3;
     }
    
     if(x==Up && x==Left) {
      MoveLeft();
      return 4;
     }
    
     return 0;
    }
    Didint work. I dont see why it doesnt detect either initialy, but it their any way to detect both arrows?

    [edit]
    Forgot to include the arrow detecting code -,-.
    Last edited by Blackroot; 01-31-2006 at 02:29 AM.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you cannot press both arrow keys at the same time. That won't work with ANY program. But, what you can do is save in another variable which up or down arrow key was last pressed then use that info to determine what direction to move when a left or right arrow key is pressed.

  3. #3
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    yes, you will get up-arrow and down-arrow keyboard codes in rapid succession (and not necessarilly in sequence!), not some combined code for both keys.

    The only keys generating those are the alt, shift, and control keys (and now the Win keys as well on newfangled keyboards produced over the last decade or so).

  4. #4
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    it seems you have overfunctionitis

    Why not just do
    Code:
     if(GetAsyncKeyState(VK_UP)&SHRT_MAX)
      //move up
     if(GetAsyncKeyState(VK_DOWN)&SHRT_MAX)
      //move down
     if(GetAsyncKeyState(VK_LEFT)&SHRT_MAX)
      //move left
     if(GetAsyncKeyState(VK_RIGHT)&SHRT_MAX)
      //move right
     if(GetAsyncKeyState(VK_ESCAPE)&SHRT_MAX)
      //quit
    You've reduced the size, not lost functionality, and can now handle any number of keys at the same time.

  5. #5
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    That would boost how fast it goes, but I call the IsArrowDown function for other reasons. (Like the menu) so it becomes inconvenient to write the same code several times.

    So are you saying it will never return both arrows at once? But then why is it not returning arrows period? Thank you for your answers, I will try and adjust my code a bit.

    [edit] Is it possible to return an int array with a function? That way no arrow would have return priority.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Is it possible to return an int array with a function?
    Not really, but you could pass in an array, use a global array, use a container class (like vector), or write your own wrapper class.

    If you really want to return an int/bool array, you could allocate it dynamically.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    I think a class would be the easiest sence I dont know how to use vectors, and a global would be a memory waster :P. Why didint I think of that? -,-.

    Alright, I think making my arrow function return ints per down arrow will help things, thanks!
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Platform game gravity problem
    By Akkernight in forum Game Programming
    Replies: 33
    Last Post: 02-22-2009, 01:10 PM
  2. overhead of virtual functions
    By coletek in forum C++ Programming
    Replies: 4
    Last Post: 01-12-2009, 12:56 PM
  3. Declaring instances of different implementations
    By dwks in forum C++ Programming
    Replies: 8
    Last Post: 07-16-2008, 11:43 AM