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

Author Topic: Sprite in multiple cpp files  (Read 1311 times)

0 Members and 1 Guest are viewing this topic.

Jan666

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Sprite in multiple cpp files
« on: May 15, 2022, 11:45:04 pm »
Hi, i got a question. I have a Sprite call "plrSprite" in the player.h file and player.cpp file, but i need to use it in my button.cpp.file to move it, know someone a solution?
Player.h file:
...
private:
       
        Sprite plrSprite; //i need this sprite
        Texture plrText;
        Image plrImage;
...
 

Player.cpp file:

...
void player::plrMove(){
               
        plrSprite.move(3, 0);   // with this function
}
...
 

Button.cpp file:
void buttons::btnTouch(RenderWindow &window){
                               
sf::Vector2i pixelPos = sf::Touch::getPosition(0, window);
sf::Vector2f worldPos = window.mapPixelToCoords(pixelPos);

btnRect1 = btnSprite1.getGlobalBounds();
       
        if(btnRect1.contains(worldPos)){
               
        btnSprite1.setTexture(btnText2);       

                            // i need it here !!!!!!!!!!!!!!!!!!!!
        }
        else{          
                btnSprite1.setTexture(btnText);        
        }  
       


 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11029
    • View Profile
    • development blog
    • Email
Re: Sprite in multiple cpp files
« Reply #1 on: May 19, 2022, 12:57:31 pm »
The button has no relation to the player's sprite, as such it shouldn't directly move the player sprite, instead the player should expose a function that internal moves its sprite.

Then as for the button "connecting" to the player, there are many approaches possible. A common style is to work with callbacks, so you'd pass in a lambda or similar, but that solution might be a bit too complex if you're no familiar with it.
As such as alternative, you could pass in the player to the button constructor, so the button knows about the player and can react to it.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Jan666

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Re: Sprite in multiple cpp files
« Reply #2 on: May 20, 2022, 06:57:15 am »
Thanks for your Answer, i fixed it, i let the sprite move in the main.cpp and only take the values from the other files