SFML community forums

Help => General => Topic started by: 5gum on December 07, 2013, 11:48:27 pm

Title: Change a Sprite in a function?
Post by: 5gum on December 07, 2013, 11:48:27 pm
Hey,

I wrote a function similar like:

int more(sf::Sprite rect)
{
  if(sf::Keyboard::isKeyPresed(sf::Keyboard::Up))
  {
    rect.scale(1.1,1.1)
    return 1;
  }
return 0;
}

(...)

int return = more(rect);
 

But now my rectangle doesn't change.  I think this is because you can return only 1 value (here it is the int). Is there any simple solution for my problem? Sry, I'm a beginner ;)
Don't say "Why you need a function for that", the real function is much bigger...

Thanks,
5gum
Title: AW: Change a Sprite in a function?
Post by: eXpl0it3r on December 08, 2013, 12:14:51 am
Well the code is in many ways bad, but if one would ignore that, you might want to pass the sprite by reference instead of by value.
Title: Re: Change a Sprite in a function?
Post by: 5gum on December 08, 2013, 12:27:12 am
Thanks, but how I'm doing that?
Title: Re: Change a Sprite in a function?
Post by: G. on December 08, 2013, 01:18:31 am
If you don't know this very basic thing, stop what you're doing immediately with SFML and learn C++ for a few weeks.
Title: Re: AW: Change a Sprite in a function?
Post by: The Hatchet on December 08, 2013, 02:27:33 pm
Well the code is in many ways bad, but if one would ignore that, you might want to pass the sprite by reference instead of by value.

Indeed.  All your code is doing is creating a copy of the sprite in the function, changing the copies scale then deleting the copy as it exits the function leaving the original sprite untouched.
Title: Re: Change a Sprite in a function?
Post by: KarmaKilledtheCat on December 10, 2013, 04:14:44 am
try:
int more(const sf::Sprite& rect)

Also, pass the sprite's bounting box instead with the sf::Sprite::getGlobalBounds function.

and what is this?:
int return = more(rect);

I'm surprised that line doesn't create an error. You don't give a return statement a variable type.