Actions on pressing a key

Started by
3 comments, last by Juanu 18 years, 7 months ago
Hi all! I've been having a question that cant' figure out How could i do to make mi characters make diferent actions at a given time, eg: With CTRL press, it animates he sprites for the character to dance. Just an example teh thing is i need to animate an entire animation with teh sprites with onle pressing the key It's not the same as moving, since it only moves WHILE it's pressing the key.. Any ideas on this? Thaanx a lot By the way.. I'm USing SDL, but i think this should be relevant for any liubrary... am i wrong?
Advertisement
the manual is your friend. this page will be of help.
This space for rent.
I already know how to handle an event

But this is teh difference
I want to know how to execute an action When a key is Pressed and not WHILE a key y pressed.

For instance:

You have a character, that moves WHILE you press the arrows and stoips when you release them, but the character jumps WHEN you press the jump key, and that action of jumping keeps going unitl the character reaches the floor no matter if you hold the key or not..

See the difference?
Let's say you have your game loop that looks like this:
bool done = false;SDL_Event Event;while( !done ){   while ( SDL_PollEvent(&Event) )   {       ...       if ( Event.type == SDL_QUIT )       {           done = true;	   break;       }       ...   }   // Clears the screen   SDL_FillRect( SDL_GetVideoSurface(), 0, 0 );   Update();   Draw();   // Flip the screen   SDL_Flip( SDL_GetVideoSurface() );}


Now focusing on the update function, this is where you will want to do what you are trying to get done.

void Update(){   Uint8* keys = SDL_GetKeyState(0);   if( keys[SDLK_RIGHT] )      {      /* For this if statement, as long as the user holds down the right arrow key, the code in here will be executed. So this style is useful for continous movement. */   }   if( keys[SDLK_a] )      {      keys[SDLK_a] = 0;      /* For this if statement, it's the OPPOSITE of what happens previously. No matter how long the A key is held down, the code will only be executed once per "key release" */   }}


So if you want the player to only animate when a key is held down, then you will want to use the first style of if statement for input. If you want to cycle though and animation one at a time, then you will want to use the second style. The whole "clearing" of the keys[index] is what makes the two very different.

So some pseudo code for what you have talked about:
void Update(){   Uint8* keys = SDL_GetKeyState(0);     if( keys[SDLK_RCTRL] && Player.canjump == true )   {      Player.Jump = true;      keys[SDLK_RCTRL] = 0;      Player.canjump = false;   }}void Draw(){   if( Player.Jump )      Player.ContinueJump;   else      Player.canjump = true;}


It also depends on how you have your actions setup for you class as well.
thanx!!

I'll giveit atry.... tomorrow...
for now,.. im have to go to bed

BUT I really apreciate içt.

See ya!

This topic is closed to new replies.

Advertisement