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

Author Topic: Changing music when Game State changes  (Read 12307 times)

0 Members and 1 Guest are viewing this topic.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Changing music when Game State changes
« Reply #15 on: August 19, 2013, 07:42:29 pm »
There's no point using events for that.  Write a function that detects collisions, call that in the window loop (since that's something you actually do want happening constantly), and when it does detect a collision then it will change the state (possibly by calling another function).

For the music distortion, try using your debugger to see what audio functions are getting called when you click again.  Also, did you mean to write if(CurrentState == PLAY)? Because that looks like it should be a !=.

WDR

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
Re: Changing music when Game State changes
« Reply #16 on: August 19, 2013, 08:09:16 pm »
Yes, I did write a function for game state change when collisions occur. And it executes as desired. My actual question was... How do I implement music in that function? Do I need an event for that too? Sorry for not phrasing that correctly before.  ;D

And the if(CurrentState == PLAY) condition... If I execute the code without using that condition, it changes music when I click the mouse anywhere on the screen, even if there is no state change between MENU and PLAY. So, I wrote that to play music only when the state changes. Should I maybe use if(CurrentState != MENU)? But then, there are three other states that are != MENU. Please clarify these for me. Thanks.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Changing music when Game State changes
« Reply #17 on: August 19, 2013, 09:38:03 pm »
...you just call the same audio functions you did before? Like play()?  If you're asking about variable scope, then just make a global variable for your game's music so you can change it from anywhere.  If you know C++ that should go without saying.

You may have heard that global variables are evil, but for a little toy program like this it's fine.  As you develop your game eventually you'll hit a point where the downsides of globals become annoying and it makes sense to put the global music object inside a class or something.


For the condition, that's also making it sound like you don't know C++.  Obviously, the code under if(event.type == sf::Event::MouseButtonPressed) gets executed every time a mouse click happens, so if you have no condition at all, obviously the music changes every time you click.  If you want it to change when the game is in the PLAY state and you click, then use an if(CurrentState == PLAY) statement.  But since that music changing code stops the menu music and starts the play music, it looks like you intend it to be executed when the game switches to PLAY mode.  Whatever you intend it to do, that's what you should write.  I only asked about that line because it didn't appear to match what you said you wanted.
« Last Edit: August 19, 2013, 09:40:36 pm by Ixrec »

WDR

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
Re: Changing music when Game State changes
« Reply #18 on: August 19, 2013, 11:10:13 pm »
OK... Yeah, I got what you said. I am familiar with those terms, only not in detail. I'm still a learner. But, I understand what you said. Still at the learning stage, so... baby steps... baby steps...

I will go into detail on what you said and implement it. Thank you. :)

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Changing music when Game State changes
« Reply #19 on: August 20, 2013, 01:22:09 am »
Incidentally, based on the code snippets I saw before--and assuming you want your actual game to have similar code organization after you put some real content into it--it occurred to me a class like this might be handy.

class GameState {
  Menu MainMenu;
  Play PlayGame;
  typedef enum {MENU, PLAY} state_type;
  state_type state;
  std::map<state_type, sf::Music> musics;

public:

  GameState() {
    //load all the musics, do whatever you need to do to setup the Menu and Play objects properly
    state = MENU;
    musics[MENU].play();
  }

  draw_me() {
    switch(state) {
    case MENU:
      MainMenu.Show(window, menu, play, quit, CurrentState);
      break;
    case PLAY:
      PlayGame.Show(window, background, player, CurrentState);
      break;
    }
  }

  switch_state(state_type new_state) {
    switch(new_state) {
    case MENU:
      musics[state].stop();
      state = MENU;
      break;
    case PLAY:
      musics[state].stop();
      state = PLAY;
      break;
    }
  }

};
 

Just spent a minute scribbling this down, no testing or anything, so no guarantees.

WDR

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
Re: Changing music when Game State changes
« Reply #20 on: August 20, 2013, 04:36:30 pm »
Oh... Thank you for that. I'll definitely check it out. My code is kinda messy now. I do have to make it OO. This could help when I do that. Thanks, again. This topic actually cleared quite a lot of my doubts.  :)