Jump to content
  • Advertisement
Sign in to follow this  
Juanu

Actions on pressing a key

This topic is 4809 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
thanx!!

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

BUT I really apreciate içt.

See ya!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!