Jump to content


problem with a bit of code


9 replies to this topic

#1 neilsamtani

    New Member

  • Members
  • PipPip
  • 24 posts

Posted 03 August 2003 - 12:36 AM

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

#2 Ed Mack

    Senior Member

  • Members
  • PipPipPipPip
  • 1239 posts

Posted 03 August 2003 - 04:13 AM

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

#3 neilsamtani

    New Member

  • Members
  • PipPip
  • 24 posts

Posted 03 August 2003 - 12:19 PM

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 :)

#4 donBerto

    Senior Member

  • Members
  • PipPipPipPip
  • 369 posts

Posted 03 August 2003 - 03:05 PM

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:
Imagine.

#5 davepermen

    Senior Member

  • Members
  • PipPipPipPip
  • 1306 posts

Posted 03 August 2003 - 07:57 PM

that keystate array is cool!

anyways, i try to stay on the eventbased way.. lalala encapsulation, eventdriven lalala.. blah:D
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....

#6 neilsamtani

    New Member

  • Members
  • PipPip
  • 24 posts

Posted 04 August 2003 - 01:45 AM

: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

#7 davepermen

    Senior Member

  • Members
  • PipPipPipPip
  • 1306 posts

Posted 04 August 2003 - 06:25 AM

hm.. interesting issues.. the SDL_KEYDOWN message always worked for me.. :|
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....

#8 Ed Mack

    Senior Member

  • Members
  • PipPipPipPip
  • 1239 posts

Posted 04 August 2003 - 07:27 AM

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.

#9 neilsamtani

    New Member

  • Members
  • PipPip
  • 24 posts

Posted 04 August 2003 - 12:20 PM

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

#10 davepermen

    Senior Member

  • Members
  • PipPipPipPip
  • 1306 posts

Posted 04 August 2003 - 01:12 PM

i guess you messed something up with your switch code.. or so.. dunno
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users