Jump to content
  • Advertisement
Sign in to follow this  
MPG

Having trouble making a laser effect with SDL_Event

This topic is 4552 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
Advertisement
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
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!