Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Wenzil

Member Since 01 Aug 2012
Offline Last Active May 19 2015 05:51 PM

#5098983 Using global variables - really so bad?

Posted by Wenzil on 05 October 2013 - 11:51 AM

Inject the sound system in Level1's constructor.
 
public int main()
{
  SoundSystem* soundSystem = new SoundSystem();
  Level1* level1 = new Level1(soundSystem);
}

class Level1
{
private:
  Player player;
  SoundSystem* soundSystem;
public:
  Level1(SoundSystem* soundSystem) : soundSystem(soundSystem)
  {
  }
  void Logic()
  {
     if(something) soundSystem->Play("shot.wav");
  }
};




#5050459 OOP Newb: Am I doing this right?

Posted by Wenzil on 05 April 2013 - 06:49 PM

This is just a general OOP guideline, but it fits this particular example very well. According to Law of Demeter (see http://en.wikipedia.org/wiki/Law_of_Demeter), the clients of your Player class (in this case, Game) should not know about the Sprite subcomponent. So in your Player class you would have a Move(float x, float y) method defined like this:

 

void Player::Move(float x, float y) {
    sprite.move(x, y)
}

 

Then, inside Game.cpp, instead of moving the player's sprite directly, you just tell the player to move, and the player will take care of moving its sprite.

 

Similarly, you should define the Player constructor to set the sprite's origin and initial position, instead of having Game.cpp do it.

 

 




PARTNERS