Sign in to follow this  

Having trouble making a laser effect with SDL_Event

This topic is 4335 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

As of now I have to hold down the space key to make the image move. how do I make it so that when you press the key once it will move automatically?
if ( global.type == SDL_KEYDOWN )
      {
        switch ( global.key.keysym.sym)
		{

case SDLK_SPACE:
	{
     laser.y = laser.y - 5;
	 SDL_BlitSurface(helaser,NULL,screen,&laser);

	}
	  } 

Share this post


Link to post
Share on other sites
EDIT: Totally misread what you said.

class var: bool SpacebarPushed = false


// if they press the space bar
if (!SpacebarPushed && SDL_Spacebar_Down)
SpacebarPushed = true;

if (SpacebarPushed)
doNeatStuff();


Share this post


Link to post
Share on other sites


SDL_Surface *helaser = ...;
laserx = ...;
lasery = ...;
laserVelocityX = 0;
laserVelocityY = 0;

// main loop
while( done == false )
{
// in Poll loop
case SDLK_SPACE:
{
laserVelocityY = -5;
}

// outside poll loop
laserx += laserlaserVelocityX;
lasery += laserVelocityY;
laser.x = laserx;
laser.y = lasery;
SDL_BlitSurface(helaser,NULL,screen,&laser);
SDL_Flip(...);
}





It would be better to use structs/classes for the positions and velocities.

Don't use the SDL_Rect you pass to SDL_BlitSurface() to store positions, the rect will be modified to show the area of the sprite that was actually blitted.

Share this post


Link to post
Share on other sites
Quote:
Original post by MPG
I don't understand laserx = ...;
lasery = ...;


I meant that you put the starting positions of the laser in. I don't know what they are.

Sometimes when I use "..." in code examples it means fill in your code here.

Share this post


Link to post
Share on other sites

This topic is 4335 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this