Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualBeerNutts

Posted 29 November 2012 - 01:21 PM

My suggestion would be to give your character a velocity when certain buttons have been pressed. Something like this (pseudo-code):

#define DEFAULT_SPEED	 10

// If a key is pressed, give it an initial velocity
if (Key.IsPressed) {
  // handle X movement
  if (Key.Type == RIGHT) {
	Player.xVelocity += DEFAULT_SPEED;
  }
  else if (Key.Type == LEFT) {
	Player.xVelocity += -DEFAULT_SPEED;
  }

  // Handle Y movement
  if (Key.Type == UP) {
	Player.yVelocity += -DEFAULT_SPEED;
  }
  else if (Key.Type == DOWN) {
	Player.yVelocity += DEFAULT_SPEED;
  }
}

// When a key is release, subtract that same velocity
if (Key.IsReleased) {
  if (Key.Type == RIGHT) {
	Player.xVelocity -= DEFAULT_SPEED;
  }
  else if (Key.Type == LEFT) {
	Player.xVelocity -= -DEFAULT_SPEED;
  }
  if (Key.Type == UP) {
	Player.yVelocity -= -DEFAULT_SPEED;
  }
  else if (Key.Type == DOWN) {
	Player.yVelocity -= DEFAULT_SPEED;
  }
}

// When moving, just move the player by their veloctities in both directions)

Good luck!

#1BeerNutts

Posted 29 November 2012 - 01:20 PM

My suggestion would be to give your character a velocity when certain buttons have been pressed. Something like this (pseudo-code):

#define DEFAULT_SPEED     10

// If a key is pressed, give it an initial velocity
if (Key.IsPressed) {
  if (Key.Type == RIGHT) {
    Player.xVelocity += DEFAULT_SPEED;
  }
  else if (Key.Type == LEFT) {
    Player.xVelocity += -DEFAULT_SPEED;
  }

  if (Key.Type == UP) {
    Player.yVelocity += -DEFAULT_SPEED;
  }
  else if (Key.Type == DOWN) {
    Player.xVelocity += DEFAULT_SPEED;
  }
}

// When a key is release, subtract that velocity
if (Key.IsReleased) {
  if (Key.Type == RIGHT) {
    Player.xVelocity -= DEFAULT_SPEED;
  }
  else if (Key.Type == LEFT) {
    Player.xVelocity -= -DEFAULT_SPEED;
  }
  if (Key.Type == UP) {
    Player.yVelocity -= -DEFAULT_SPEED;
  }
  else if (Key.Type == DOWN) {
    Player.xVelocity -= DEFAULT_SPEED;
  }
}

// When moving, just move the player by their veloctities in both directions)

Good luck!

PARTNERS