SFML community forums

Help => Graphics => Topic started by: mbuckley2000 on December 16, 2013, 07:45:38 pm

Title: Why doesn't this work?
Post by: mbuckley2000 on December 16, 2013, 07:45:38 pm
Hey,

I am trying to set one sprites texture to be the same as another's, but it wont compile..

Like this:
Sprite.setTexture(Sprite2.getTexture);

I also tried:
Like this:
Sprite.setTexture(Sprite2.getTexture());


Any help would be greatly appreciated :)
Title: Re: Why doesn't this work?
Post by: Nexus on December 16, 2013, 07:51:50 pm
The types don't match (pointer vs reference). I'm sure the compiler message would tell that, but you have to actually read it.

And please have at least a look at the documentation (http://www.sfml-dev.org/documentation/2.1/) before asking such questions...
Title: Re: Why doesn't this work?
Post by: mbuckley2000 on December 16, 2013, 08:17:04 pm
I did read it.. I suppose what I should have asked is how can I perform this action without creating more images?
Title: Re: Why doesn't this work?
Post by: The Terminator on December 16, 2013, 08:39:56 pm
I did read it.. I suppose what I should have asked is how can I perform this action without creating more images?

If you read it, then you'd ought to familiarize yourself with pointers and references. Plus, judging that you tried to call a function without parentheses makes it quite obvious you don't know the basics of C++. I'd recommend getting a good C++ book. To answer your question, sf::Sprite::setTexture takes a reference to a const Texture, and sf::Sprite::getTexture returns a pointer to a const Texture. For your second try, you're passing a pointer as an argument instead of a reference.
Title: Re: Why doesn't this work?
Post by: mbuckley2000 on December 16, 2013, 10:14:04 pm
OK, thanks for the advice.. I was planning to try and learn on the job as I have coded games previously in other languages but I will take your advice and learn more of the theory first :)
Title: Re: Why doesn't this work?
Post by: wintertime on December 17, 2013, 09:00:55 am
You should have kept the Texture around anyways to prevent the white box problem, so you should not need to ask the Sprite for it and be able to directly use it for the other Sprite.
http://sfml-dev.org/tutorials/2.1/graphics-sprite.php