1

I have done a map with Tiled. Every single tile of it measures 32x32 px so does my main character sprite.

In my class Player.cpp, I have some functions that calculates a deltaY/X which decide if you are going Left/Right/Up/Down and then you use them in the update function.

That's my code so far:

void Player::moveX(int moveDirection) {
    deltaX = moveDirection * PlayerConstants::WALK_SPEED;
    playAnimation(deltaX > 0 ? "WalkRight" : "WalkLeft");
    facing = deltaX > 0 ? DIRECTION ::RIGHT : DIRECTION::LEFT;
}

void Player::moveY(int moveDirection) {
    deltaY = moveDirection * PlayerConstants::WALK_SPEED;
    playAnimation(deltaY < 0 ? "WalkUp" : "WalkDown");
    facing = deltaY < 0 ? DIRECTION ::UP : DIRECTION::DOWN;
}

void Player::update(float elapsedTime) {
    _x += deltaX * elapsedTime;
    _y += deltaY * elapsedTime;

    AnimatedSprite::update(elapsedTime);
}

Walk Speed is 0.1 right now.

Any guesses on how to move it tile by tile?

genpfault
  • 51,148
  • 11
  • 85
  • 139
James
  • 95
  • 1
  • 9
  • is the value of 'moveDirection' either -1 or 1? – travisjayday Jul 27 '17 at 13:28
  • @travisjayday indeed it is! – James Jul 27 '17 at 13:30
  • in your code, I'm guessing that facing is a global variable? Typically, it's not good to use global variables, because when you write software it can be become unclear what functions are changing that global variable. You might later want to remember how that global variable is getting changed( in case of bugs ), good luck tracing it back to those functions. Instead you could return facing easy enough. – Keith Becker Jul 27 '17 at 13:55
  • @KeithBecker well, facing is a private variable from the class Player, not global at all (if i understand what you're saying) – James Jul 27 '17 at 14:17
  • @James Oh, yeah that's right, never mind then – Keith Becker Jul 27 '17 at 16:07

1 Answers1

1

Here's what you could do.

Create 2 global variables called, say, x_overflow and y_overflow. Now, in your update function, instead of directly adding deltaX and deltaY to _x and _y, add deltaX and deltaY to x_overflow and y_overflow. Then, add a couple if-statments checking if x_overflow and y_overflow are greater than your desired distance (i'm guessing 32px), and if they are, move your character by 32px.

Something like this

void Player::update(float elapsedTime) {
x_overflow += deltaX * elapsedTime;
y_overflow += deltaY * elapsedTime;

if (x_overflow >= 32) {
    _x += 32;
    x_overflow = 0;
}
else if (x_overflow <= -32) {
    _x -= 32;
    x_overflow = 0;
}

if (y_overflow >= 32) {
    _y += 32;
    y_overflow = 0;
}
else if (y_overflow <= -32) {
    _y -= 32;
    y_overflow = 0;
}

AnimatedSprite::update(elapsedTime);
}

I haven't really tested this code (i.e. don't copy paste), but you should understand the main idea behind it and implement it yourself. Good Luck!

travisjayday
  • 784
  • 6
  • 16
  • It does make sense. I will give it a try right now and as soon as it works I'll let you know :) Thanks! – James Jul 27 '17 at 13:50
  • it actually work! It does not look very smoothy at the moment, but I'll think of how to fix it :)! Thanks – James Jul 28 '17 at 14:41
  • @James Glad to hear! If the answer helped you, please consider marking it as the answer (press the checkmark and upvote). It'd help my rep ;) – travisjayday Jul 28 '17 at 14:44