problem with a bit of code

Ae450d9a70144c898c04af522771f4de
0
neilsamtani 101 Aug 03, 2003 at 00:36

Hey guys, been a while….

Hope things are all good & the game is progressing….been away working on my game & have come across a problem. I am using SDL, and am trying to make my little sprite man jump correctly…..it seems that although the man jumps up when the ‘up’ button is pressed down, he does not move to the right/left during the jump until the key is lifted….as you can imagine it makes him move only 1 pixel each time!!

I’d really appreciate any help - the code is listed below (you guys will prob. laugh)

while(char_jump==1)
{
    SDL_Event event;
    while (SDL_PollEvent(&event))
    {

 if (event.key.keysym.sym == SDLK_UP)
 { 
    velocity=-15;
    jumping=1;

  while(jumping==1)
    {
  velocity=velocity+accel;
  walkright.mY=walkright.mY+velocity;

  while(SDL_PollEvent(&event))
    {   
   
   if((event.type == SDL_KEYUP) && (event.key.keysym.sym == SDLK_LEFT))
   {DrawWalkLeft();}
     
   if((event.type == SDL_KEYUP) && (event.key.keysym.sym == SDLK_RIGHT)) 
    {walkright.xadd(1);}

    }

    if (structure[walkright.mX+8][walkright.mY+24]==1)

   {
    velocity=0;
    jumping=0;
    char_jump=0;
    DrawScene();
   }

after that it’s really just closing the } and drawsceneing it.

cheers & thanks in advance!

Neil

9 Replies

Please log in or register to post a reply.

065f0635a4c94d685583c20132a4559d
0
Ed_Mack 101 Aug 03, 2003 at 04:13

The problem seems to be you have two whiles checking the Event stack, one inside the other. The first one filters for Key ups, then the one inside also looks for them.. It would be a lot easier to do something like this:

while (SDL_PollEvent(&event)) {
  switch (event.type) {
   case SDL_KEYDOWN:
    switch (event.key.keysym.sym) {
  case SDLK_UP:
   // something
   break;
  case SDLK_LEFT:
   // something else
   break;
    }
    break;
  }

But, I prefer to use the Keystate array in sdl:

 keystate = SDL_GetKeyState(NULL);

 if(keystate[SDLK_LEFT]) angle -= 0.05;
 if(keystate[SDLK_RIGHT]) angle += 0.05;

I’m sure you get the idea

Ae450d9a70144c898c04af522771f4de
0
neilsamtani 101 Aug 03, 2003 at 12:19

Hey Ed,

Thanks v. much for that - it’s funny, but I tried the keystate function before I did the eventstate, but it didn’t seem to work! this time it worked well, thanks for that mate!

See ya,
Neil :)

C24eb7e6aaefba78b94c831ddc7b4d0b
0
donBerto 101 Aug 03, 2003 at 15:05

Ed Mack: thanks a lot!

I’ve been working on an input handler class and you know how once you have something going, you often don’t look for other solutions? well, to make a long story short, I made my own buffer array for all input and although I got the results desired, input seemed to lag. I can type something fast and the characters would only come a bit later.

thanks to the realization of the keystate array [that you pointed out], I should be able to fix this problem.

again, thanks.
:yes:

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Aug 03, 2003 at 19:57

that keystate array is cool!

anyways, i try to stay on the eventbased way.. lalala encapsulation, eventdriven lalala.. blah:D

Ae450d9a70144c898c04af522771f4de
0
neilsamtani 101 Aug 04, 2003 at 01:45

:lol:

finally got my sprite guy jumping around like a frog on speed!

don’t know if anyone will still be interested, but what I did at the end was use the keystate array to check if up was being pushed (even with the case statement the keydown was not being found, only when it was being lifted up!) . That worked fine, but now I had to find if the left/right was being pushed! I was stuck because trying to establish another keystate was not working, and the keys were not being found. However, tried putting the following before the Uint8* et voila, it was refreshing the keystate!

SDL_Event new_event;
SDL_PollEvent(&new_event);

Guess my code just got a lot better though, thanks to the whole keystate array, thanks again guys!

Later
Neil

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Aug 04, 2003 at 06:25

hm.. interesting issues.. the SDL_KEYDOWN message always worked for me.. :|

065f0635a4c94d685583c20132a4559d
0
Ed_Mack 101 Aug 04, 2003 at 07:27

Yeah, of course keyState is just a matter of convenience, but it’s a nice neat way to add something onto a variable each frame. See www.libsdl.org manual, it’s bound to have other useful tidbits.

Ae450d9a70144c898c04af522771f4de
0
neilsamtani 101 Aug 04, 2003 at 12:20

I really couldn’t figure that out!

why was it that the event wasn’t working? I had an PollEvent at the game look to check if the program is to quit, and that works fine, but the SDL_KEYDOWN was not working until the key was lifted(the program would actually freeze until the key was lifted!).

This was happening even with the switch/case block you put up Ed. I’m confused, might go on a little side mission to figure out what had gone wrong!! But anyway, its all in order now, I’ll just have to use this until I get it in order.

Cheers,
Neil

6ad5f8c742f1e8ec61000e2b0900fc76
0
davepermen 101 Aug 04, 2003 at 13:12

i guess you messed something up with your switch code.. or so.. dunno