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

Author Topic: Change a Sprite in a function?  (Read 3872 times)

0 Members and 1 Guest are viewing this topic.

5gum

  • Newbie
  • *
  • Posts: 27
    • View Profile
Change a Sprite in a function?
« 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
AW: Change a Sprite in a function?
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

5gum

  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: Change a Sprite in a function?
« Reply #2 on: December 08, 2013, 12:27:12 am »
Thanks, but how I'm doing that?

G.

  • Hero Member
  • *****
  • Posts: 1590
    • View Profile
Re: Change a Sprite in a function?
« Reply #3 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.

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: AW: Change a Sprite in a function?
« Reply #4 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.

KarmaKilledtheCat

  • Newbie
  • *
  • Posts: 23
    • View Profile
    • Email
Re: Change a Sprite in a function?
« Reply #5 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.

 

anything