Jump to content

  • Log In with Google      Sign In   
  • Create Account


SDL how to move an object by holding a button rather then pressing the button over and over


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 bigbadbear9885   Members   -  Reputation: 177

Like
0Likes
Like

Posted 06 December 2012 - 07:55 PM

I've already tried looking this up but I can't find anything to help.
I just learned how to move an object which was successful. You press a button once to move it, but you can't hold it down to keep the object moving, you have to keep tapping the button to make move more.

Is there a certain way or certain code to make it so that you can just hold the button to keep moving and let go whenever you want to stop?

Sponsor:

#2 Servant of the Lord   Crossbones+   -  Reputation: 17272

Like
4Likes
Like

Posted 06 December 2012 - 08:29 PM

Sure, there are two common ways:

A) On button press, set bool "move" to true. On button release, set bool "move" to false. (You have to create such a bool yourself). Every frame, if "move" is true, move the player. Realistically, you'll want several bools, because you can move in several directions, or alternatively, two ints (x,y) holding the amount to move each frame, positively or negatively.

B) Let SDL handle the "move" boolean for you, and instead call SDL_GetKeyState() which returns a bool array of most of the keyboard keys and whether they are currently being held or not (see documentation link). Every frame you can then check if the key is held down, and move the player.

Edited by Servant of the Lord, 06 December 2012 - 08:30 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#3 bigbadbear9885   Members   -  Reputation: 177

Like
0Likes
Like

Posted 07 December 2012 - 12:44 AM

(sigh) Now I'm hitting some more problems, things may be a little mixxed up. Now it won't move at all! It shows but doesn't respond in movement. Heres the code I wrote if you or anyone can see what I'm doing wrong

Attached File  ship.bmp   2.05KB   49 downloads
#include "SDL.h"
int main(int argc, char *argv[])
{
bool bRun = true;
SDL_Surface *screen , *ship;
SDL_Rect shipRect;
shipRect.x = 100 ;
shipRect.y = 100 ;
	
SDL_WM_SetCaption("Fryday", NULL);
screen = SDL_SetVideoMode( 256 , 224 , 32 , SDL_DOUBLEBUF|SDL_HWSURFACE|SDL_ANYFORMAT);

SDL_FillRect(screen , NULL , 0x221122);
ship = SDL_LoadBMP("./ship.bmp");
SDL_SetColorKey( ship, SDL_SRCCOLORKEY, SDL_MapRGB(ship->format, 255, 0, 255) );
SDL_BlitSurface( ship , NULL , screen , &shipRect );
SDL_Flip(screen);
	 SDL_Event event;
  while(bRun) {
  bool keysHeld[323] = {false}; // everything will be initialized to false

if (SDL_PollEvent(&event))
	  {
		 if (event.type == SDL_QUIT)
		 {
		    bRun = false;
		 }
		 if (event.type == SDL_KEYDOWN)
		 {
		    keysHeld[event.key.keysym.sym] = true;
		 }
		 if (event.type == SDL_KEYUP)
		 {
		    keysHeld[event.key.keysym.sym] = false;
		 }

	  if ( keysHeld[SDLK_ESCAPE] )
	  {
		 bRun = false;
	  }
	  if ( keysHeld[SDLK_LEFT] )
	  {
		 shipRect.x -= 1;
	  }
	  if ( keysHeld[SDLK_RIGHT] )
	  {
		 shipRect.x += 1;
	  }
	  if ( keysHeld[SDLK_UP] )
	  {
		 shipRect.y -= 1;
	  }
	  if (keysHeld[SDLK_DOWN])
	  {
		 shipRect.y += 1;
	  }
}
    }; // while(bRun) { END
return 0;
}


#4 Cdrandin   Members   -  Reputation: 443

Like
0Likes
Like

Posted 07 December 2012 - 01:25 AM

I am a pygame person, but typically it's done as this:
if event.key == event.keydown:
   if event.ket == event.K_UP:
	   ship.location.x -= 10  // Pygame takes (0,0) top left screen MAX_width,MAX_height) to be bottom right screen

Your code handles keydown alone versus handling with some specific key being pressed AND held. Hope this helps.

Edited by Cdrandin, 07 December 2012 - 01:26 AM.


#5 RulerOfNothing   Members   -  Reputation: 1151

Like
1Likes
Like

Posted 07 December 2012 - 01:30 AM

Also, you only draw the ship once. You need to clear and redraw the ship every time it moves (also you need to include code that prevents the ship from being moved too quickly, as you are probably going to get 100+ FPS when you are only drawing one picture)

#6 Servant of the Lord   Crossbones+   -  Reputation: 17272

Like
0Likes
Like

Posted 07 December 2012 - 02:41 PM

As RulerOfNothing says, your game loop needs to be setup like this:
  • Load resources
  • Initialize game settings
  • While (still playing)
  • Get all the player input (if while(SDL_PollEvent(&event)) )
  • Update the game objects (by deltaTime*)
  • Do any game thinking (optionally part of the updating code instead)
  • Render:
  • Clear the entire screen
  • Draw every object from back to front
  • Draw the GUI
  • Flip the screen buffers
[*]Uninitialize the game, if neccesary.
[*]Free game resources
[/list]*A simple deltaTime is 'currentTime - previousTime'. This is the amount of time passed since the last frame. See SDL_GetTicks(). Others prefer a fixed timestep, but for beginner projects, 'currentTime - previousTime' might be simpler, and works just fine.

Your game should follow that basic pattern. 99% of games follow that basic pattern (with minor tweaks and variations here or there).

You should go through Lazy Foo's SDL tutorials.
Particularly:But the tutorials build off each other, so you really really should go through them from the start.
You also should read Lazy Foo's article on Game Loops, and if you have any questions, ask here and we'll be glad to help.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#7 Poigahn   Crossbones+   -  Reputation: 516

Like
0Likes
Like

Posted 08 December 2012 - 05:43 AM

BigBadBear - I am no expert in the lanquage you are using, but ( IF ) I am reading your code correctly ( THEN ) in your IF Keysheld statement, Romove The Equal Sign ( = ) after your Plus/Minus ( + / - ) signs and your ship should move.

The language I use has KeyDown(Upkey) vs. KeyHit(Upkey) to test whether a key is held or simply pressed.

Your Brain contains the Best Program Ever Written : Manage Your Data Wisely !!


#8 Servant of the Lord   Crossbones+   -  Reputation: 17272

Like
2Likes
Like

Posted 08 December 2012 - 11:30 AM

@OP: The problem with your code is that:
A) You do your event looping in an if() instead of a while()
B) You don't ever actually initialize keyHeld[], leaving it set as false, thus never triggering the movement.
C) You have your keyHeld[] code inside your event loop when it should be outside. (Polling input states should usually be outside the event loop, handling input events should be inside the event loop)

BigBadBear - I am no expert in the language you are using, but ( IF ) I am reading your code correctly ( THEN ) in your IF Keysheld statement, Romove The Equal Sign ( = ) after your Plus/Minus ( + / - ) signs and your ship should move.

Not in C++. Posted Image

var1 + var2 doesn't change the result of 'var1', you'd have to do var1 = var1 + var2. Using += is shorthand for that. var1 += var2 does change var1 properly.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#9 Poigahn   Crossbones+   -  Reputation: 516

Like
0Likes
Like

Posted 08 December 2012 - 03:21 PM

Thank you SOTL. That ia why I Come here to learn and expand my knowlege. After reading your reply to my quote, I Checked it out. I think what I was thinking that since BigBadBear was only increasing or decreasing by 1, My trend of thought was the Increment / decrement operators in C++ of ++ or -- Example Ship.x ++ or Ship.x -- in order to increase / decrease the Ship.x by a value of 1.

Your Brain contains the Best Program Ever Written : Manage Your Data Wisely !!


#10 BeerNutts   Crossbones+   -  Reputation: 2576

Like
0Likes
Like

Posted 10 December 2012 - 01:04 PM

Something else to consider would be to check the Input every frame; if a key is down, mark it in an Input Object, if it's not leave blank (false); then, you can pass the Input object to anyone who wants to know about Input. Like this (pseudo-code):

struct TInput {
  bool IsLeftPressed;
  bool IsRightPressed;
  bool IsUpPressed;
  bool IsFirePressed;
  bool IsJumpPressed;
};

// create this funciton to get the current keys being pressed
void GetInput(TInput& input)
{
  while(PollKeyEvent(&keyEvent) {
    if (keyEvent.IsPressed) {
      if (keyEvent.keycode == LEFT_KEY) {
        input.IsLeftPressed;
      }
      if (keyEvent.keycode == UP_KEY) {
        input.IsUpPressed;
      }
      // Continue checking them all
    }
  }
}

// in Main loop
while(IsRunning) {
  TInput input;
  GetInput(input);

  // now pass the input into your Update functions, which would check input.IsFirePressed to see if the user is pressing fire, etc.
  Player.Update(input);

  // Call other update functions for enemies, objects, background, etc as well as rendering for them.

Edited by BeerNutts, 10 December 2012 - 01:06 PM.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS