Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Can move up but not down.  (Read 2489 times)

0 Members and 1 Guest are viewing this topic.

Bonediggerninja

  • Newbie
  • *
  • Posts: 10
    • View Profile
Can move up but not down.
« on: August 22, 2017, 07:59:13 am »
 
       
sf::Vector2f buttonCheck() {
        sf::Vector2f key = sf::Vector2f(0, 0);
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
                key.x -= 1;
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
                key.x += 1;
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
                key.y -= 1;
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
                key.y += 1;
        }

        return key;
}

int yReset(int yPos) {
       
//     Yup, literally nothing
        return yPos;
}



charSprite.move(0.25 * keyCheck.x , 0.25 * keyCheck.y);
charSprite.setPosition(charSprite.getPosition().x, yReset(charSprite.getPosition().y )
//Where keycheck is the return of the button check function.


);

However, if I remove the yReset, I can move down.

Why?
Any help is appreciated.

HughPH

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Can move up but not down.
« Reply #1 on: August 22, 2017, 09:58:54 am »
I don't really understand what you're trying to achieve with the 2nd charSprite line. It just doesn't make any sense. The parameters will be evaluated first:
Get the sprite's X
Get the sprite's Y

Then those will be passed back in to Set Position.

You're just telling the sprite to be where it already is.

Remove that line and see if matters improve.

Additionally, moving the sprite by 0.25 probably won't get it very far. SFML uses Vector2f because of OpenGL peculiarities, but positions need to be represented by whole numbers. And 0.25 * 1 will round down to 0. Try taking out those multiplications.

If you're making a game with this, moving your sprite in this manner will look pretty weird: you'll have to wait for the key repeat delay before the sprite starts moving. But take it one step at a time. Once you've cracked this problem, you might consider setting a flag when the key is pressed and resetting the flag when it's released. In your main loop, if the flag is set, you move the sprite by n. If the flag is not set, you stop moving the sprite. Then it can get a bit more interesting and you can use a velocity.

Down to why your setPosition line might be working with the call to yReset, it might be some side effect of the way the compiler is building your code crossed with the inner workings of SFML. If the compiler can see two calls to getPosition next to each other and no expectation of change of value (if the getPosition method is marked const in the SFML source) then it will optimise out one of the calls and only call it once. When it's calling yReset, there's no telling what you've done with that method and it'll be calling getPosition twice. Now,  SFML will internally have made some change based on your call to move, and that will have changed the position.

I still don't really get how it's been working at all with moving by 0.25 though.

(If Vector2f is a struct, as it in the .Net bindings, you won't need to initialise it.
sf::Vector2f key;
will be sufficient.)

Bonediggerninja

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Can move up but not down.
« Reply #2 on: August 22, 2017, 10:29:59 am »
Thanks a lot.

The yReset function was to see if the position of the sprite was above or below a certain yPos, and if it was, to bring it back below the threshold. I had removed what it contained because it didn't affect the result. However, it was converting a float to an int, rounding it and not moving down.

Can you please elaborate on what you mean by "Key repeat delay"? It's responsive as far as I can tell.

And if I shouldn't use decimals, how should I move my character at a non-supersonic rate?


HughPH

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Can move up but not down.
« Reply #3 on: August 22, 2017, 11:24:17 am »
Sorry, I see now that it's a button check to see which keys are down, so you won't have that problem.

If it was an event handler, you would get one event when the key is first pressed, then a delay, and then more events.

I use event handlers for move, fire, mode switch. But I also use IsKeyPressed for mouse modifiers (so Ctrl+click has some different behaviour to just click) - that's in conjunction with mouse events.

HughPH

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Can move up but not down.
« Reply #4 on: August 22, 2017, 11:25:57 am »
Your character might be moving at a supersonic rate if you're polling the keys every frame. 60fps would be +60 pixels every second. You could use an external counter or (as I mentioned before) a velocity:

Code: [Select]
xVelocity = 0;
if (right key pressed) {
  xVelocity = 10;
}
if (left key pressed) {
  xVelocity = -10;
}

Your sprite.move is given the xVelocity.


Then you can get into acceleration and deceleration:

Code: [Select]
if (right key pressed && xVelocity < 10) {
  xVelocity += 3;
}
if (left key pressed && xVelocity > -10) {
  xVelocity -=3;
}
if (neither left nor right key is pressed){
  xVelocity *= 0.5;
}

« Last Edit: August 22, 2017, 11:30:43 am by HughPH »