Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!

We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Member Since 01 Aug 2012
Offline Last Active Oct 28 2014 02:09 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
  Player player;
  SoundSystem* soundSystem;
  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.