SFML community forums

Help => General => Topic started by: edeksumo on April 21, 2022, 12:55:16 pm

Title: Sprite.move()
Post by: edeksumo on April 21, 2022, 12:55:16 pm
Hi, I'm trying to create first simple game using SFML but while working on player moving I stand upon one problem, function won't work in my class, here is fragment of my code:
void Player::PlayerMoving(int offset_x_min, int offset_x_max, int offset_y, sf::View view, int speed_x, int speed_y, float cameraSpeed) {
    //it's keep player on view while camera is moving
    if (pos.x <= view.getCenter().x - offset_x_min) {
            pos = sf::Vector2f(pos.x += cameraSpeed, pos.y);
    }
   
    //pos = viev.getCenter() - sf::Vector2f(offset_x, offset_y);

    if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) && (pos.x > view.getCenter().x - offset_x_min))
    {
        // left key is pressed: move our character
        //pos = sf::Vector2f(pos.x -= speed_x, pos.y); //my version of move but it's not fluent
        sprite.move(sf::Vector2f(1.f ,0.f));
        //sprite.rotate(2); //just for testing and it's working

    }
...
 
I was looking for solution but couldn't find any.
Title: Re: Sprite.move()
Post by: Stauricus on April 21, 2022, 10:35:52 pm
are you sure the sprite isn't moving at all? have you tried checking its position every frame?
you may be moving the sprite and the view at the same time, and not noticing the movement?

also, one thing that seems weird to me is that you are checking if the keyboard is pressing LEFT, and checking if the pos.x is bigger than view.x (doesn't bigger means that its to the right side of the view?)
Title: Re: Sprite.move()
Post by: edeksumo on April 21, 2022, 11:18:39 pm
Yes, I'm sure, I was checking bigger numbers to check is it work and it didn't. And yes I'm keeping the player at the right side of the screen.
Title: Re: Sprite.move()
Post by: kojack on April 22, 2022, 07:13:48 am
Is pos being used? In this code it's being set, but is it being passed to the sprite at some point? (Such as when it renders).
If you give pos to the sprite before rendering, that would overwrite any movement made by the sprite.move call.
Title: Re: Sprite.move()
Post by: edeksumo on April 22, 2022, 10:33:45 am
You're right, that was setting position through variable pos. Thank you very much!