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

Author Topic: Sprite.move()  (Read 1591 times)

0 Members and 2 Guests are viewing this topic.

edeksumo

  • Newbie
  • *
  • Posts: 3
    • View Profile
Sprite.move()
« 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.
« Last Edit: April 21, 2022, 01:11:35 pm by eXpl0it3r »

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Sprite.move()
« Reply #1 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?)
Visit my game site (and hopefully help funding it? )
Website | IndieDB

edeksumo

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Sprite.move()
« Reply #2 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.

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Sprite.move()
« Reply #3 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.

edeksumo

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Sprite.move()
« Reply #4 on: April 22, 2022, 10:33:45 am »
You're right, that was setting position through variable pos. Thank you very much!

 

anything