• SFML White sprite problem
    2 replies, posted
I hear that SFML shows a white sprite when it has lost the image that it was meant to display. I don't know how to resolve that in my situation though. This is my Player constructor (are those this-> things necessary?) [code] Player::Player(char * Name, bool Visible) { this->imgPlayer.LoadFromFile("res/images/player/player.png"); this->sprPlayer.SetImage(this->imgPlayer); } [/code] And the class itself [code] class Player { public: sf::Sprite sprPlayer; sf::Image imgPlayer; }; [/code] In my game I add each Player created to a vector, then iterate through it with my Draw() function to draw each player. I'm not sure if that's a good way of doing it but I only started with this earlier. Game init function and draw function [code] bool Game::Init() { Player Fat("Fatty!"); Fat.SetImage("res/images/player/player.png"); vPlayers.push_back(Fat); return true; } void Game::Draw() { Window.Clear(); std::vector<Player>::iterator itr; int playeridx = 1; for(itr = vPlayers.begin(); itr != vPlayers.end(); ++itr) { Player plrToDraw = *itr; Window.Draw(plrToDraw.sprPlayer); playeridx++; } } [/code] When it draws the sprite, it's just a white sprite and no image. What do I need to change to get it to work?
this-> not needed you are copying the player class with that push_back call, instead make the vector hold pointers and clean up after like... [code]std::vector<Player*> vPlayers; Player* temp = new Player("Fatty!"); temp->SetImage("res/images/player/player.png"); vPlayers.push_back(temp);[/code] Then whenever you are done with a player you must do [code]delete vPlayers[oneYouAreGettingRidOf];[/code]
That works fine :) thanks
Sorry, you need to Log In to post a reply to this thread.