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

Author Topic: sprite.move() from my gameobject* is not working  (Read 3655 times)

0 Members and 3 Guests are viewing this topic.

Putarda

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
sprite.move() from my gameobject* is not working
« on: August 14, 2016, 03:04:12 pm »
Hello :). So i made gameobject class where is stored sprite and texture. So i when i try to move sprite from my gameobject* it's not moving (x value is not changing).

Graphics.cpp:
        void openWindow() {

                window.create(sf::VideoMode(800, 800), Game::getGameName());
                window.setVerticalSyncEnabled(true);

                while (window.isOpen()) {

                        sf::Event event;

                        while (window.pollEvent(event)) {

                                switch (event.type) {

                                case sf::Event::Closed:

                                        Game::closeGame();

                                        break;
                                }

                        }

                        window.clear();

                        GameObject::GameObject* gameobject = GameObject::getGameObject("player_gameobject");

                        sf::Sprite sprite = gameobject->getSprite();

                        sf::Texture texture = gameobject->getTexture();
                       
                        sprite.setTexture(texture);

                        sprite.move(5, 0);

                        window.draw(sprite);

                        window.display();

                }

        }
GameObject.cpp:
#include "GameObject.h"
#include <unordered_map>

namespace GameObject {

        std::unordered_map<std::string, GameObject> gameobjects;

        GameObject::GameObject() {

                name = "";

                sprite = sf::Sprite();

                texture = sf::Texture();

        }

        GameObject::GameObject(std::string gameobject_name, sf::Sprite gameobject_sprite, sf::Texture gameobject_texture, int gameobject_position_x, int gameobject_position_y) {

                name = gameobject_name;

                sprite = gameobject_sprite;

                texture = gameobject_texture;

                sprite.setPosition(gameobject_position_x, gameobject_position_y);

        }

        void addGameObject(GameObject gameobject) {

                gameobjects[gameobject.getName()] = gameobject;

        }

        void removeGameObject(std::string gameobject_name) {

                gameobjects.erase(gameobject_name);

        }

        GameObject* getGameObject(std::string gameobject_name) {

                return &gameobjects[gameobject_name];
        }

}
And my GameObject class:
        class GameObject {

        private:

                std::string name;

                sf::Sprite sprite;

                sf::Texture texture;

        public:

                GameObject();

                GameObject(std::string gameobject_name, sf::Sprite gameobject_sprite, sf::Texture gameobject_texture, int gameobject_position_x, int gameobject_position_y);

                void setName(std::string gameobject_name) {

                        name = gameobject_name;

                }

                void setSprite(sf::Sprite gameobject_sprite) {

                        sprite = gameobject_sprite;

                }

                void setTexture(sf::Texture gameobject_texture) {

                        texture = gameobject_texture;

                }

                std::string getName() {

                        return name;
                }

                sf::Sprite getSprite() {

                        return sprite;
                }

                sf::Texture getTexture() {

                        return texture;
                }

        };
I hope someone can help me  :). Thanks.
*UPDATE* i also tried gameobject->getSprite().move(3, 0); and nothing happens...
« Last Edit: August 14, 2016, 03:06:43 pm by Putarda »

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: sprite.move() from my gameobject* is not working
« Reply #1 on: August 14, 2016, 04:06:22 pm »
You're returning copies of the sprite and texture from your game object, so you only update the copy, move it, draw it, then let it go out of scope. Ideally you want to return a reference to your game object members - or even better make game objects drawable and transformable directly (replacing the vertex array with your sprite member).

Putarda

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: sprite.move() from my gameobject* is not working
« Reply #2 on: August 14, 2016, 04:28:04 pm »
Thank you for your reply. But how are sprite and texture copies? GameObject is pointer.
« Last Edit: August 14, 2016, 04:33:13 pm by Putarda »

victorlevasseur

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: sprite.move() from my gameobject* is not working
« Reply #3 on: August 14, 2016, 06:33:44 pm »
sprite = gameobject_sprite;
texture = gameobject_texture;
 

Here is the copy.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: sprite.move() from my gameobject* is not working
« Reply #4 on: August 14, 2016, 08:25:59 pm »
                sf::Sprite getSprite() {

                        return sprite;
                }

                sf::Texture getTexture() {

                        return texture;
                }
                        sf::Sprite sprite = gameobject->getSprite();

                        sf::Texture texture = gameobject->getTexture();
I would say that these copies are the ones to which fallahn was referring.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Putarda

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: sprite.move() from my gameobject* is not working
« Reply #5 on: August 15, 2016, 07:34:10 pm »
Thank you  ;D!