Slick Forums

Discuss the Slick 2D Library
It is currently Sat May 18, 2013 6:49 pm

All times are UTC




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: Sat Aug 11, 2012 2:16 am 
Offline

Joined: Sat Aug 11, 2012 2:07 am
Posts: 18
I've been trying everything to make it so each state has background music that plays on a loop.
However, when ever I try to have the music play on each individual state, it plays the first one briefly, then leaves it on the second one. I can only assume that it's because all states are loaded and played at once.

So, does anyone have any ideas on making this work?

Also, I imagine I'll come to a similar issue when it comes to sound effects, so I need some ideas on getting these things implemented, and I would love some help.

I'm pretty new to java and the slick things, so the more detail, the better.

Thanks everyone!


Top
 Profile  
 
PostPosted: Sat Aug 11, 2012 10:47 am 
Offline
Regular
User avatar

Joined: Thu May 05, 2011 8:35 pm
Posts: 231
Location: Somewhere between the bits and bytes
Make sure to play and stop the music in the enter and leave methods off your states.
The init method of all your states are called once just before the game loop starts, so its no good starting music there.

_________________
For every new problem, a new source of solutions has come to exist.


Top
 Profile  
 
PostPosted: Wed Aug 15, 2012 2:43 am 
Offline

Joined: Sat Aug 11, 2012 2:07 am
Posts: 18
Magn919 wrote:
Make sure to play and stop the music in the enter and leave methods off your states.
The init method of all your states are called once just before the game loop starts, so its no good starting music there.


enter and leave methods?
Which ones are those?


Top
 Profile  
 
PostPosted: Wed Aug 15, 2012 5:00 am 
Offline

Joined: Sat Aug 11, 2012 10:13 pm
Posts: 35
They aren't required methods, so most likely you haven't put them in your state file. Like your init is going to hold your fuel right? Like the gas container of a car if that makes sense. It is always going to be there unless you get a new car and you can fill it up with so much. Your enter is like the gas pedal which requires the state to be opened to start the music when the state appears. Your leave method is like turning off your engine. Also instead of gas pedal lets say ignition. When you turn off your car you don't wanna continue hearing your engine right. Hopefully this source code will make more sense:
Code:
public void enter() {
     <musicname>.loop(pitch, volume);
}
You don't need those 2 variables, but they are there if you wanna use them.
Code:
public void leave() {
     <musicname>.stop();
}
This will tell us to stop playing the music when we aren't in the state. This isn't the best, but hopefully I kinda explained it to you.


Top
 Profile  
 
PostPosted: Wed Aug 15, 2012 7:00 am 
Offline

Joined: Sat Aug 11, 2012 2:07 am
Posts: 18
Imposter wrote:
They aren't required methods, so most likely you haven't put them in your state file. Like your init is going to hold your fuel right? Like the gas container of a car if that makes sense. It is always going to be there unless you get a new car and you can fill it up with so much. Your enter is like the gas pedal which requires the state to be opened to start the music when the state appears. Your leave method is like turning off your engine. Also instead of gas pedal lets say ignition. When you turn off your car you don't wanna continue hearing your engine right. Hopefully this source code will make more sense:
Code:
public void enter() {
     <musicname>.loop(pitch, volume);
}
You don't need those 2 variables, but they are there if you wanna use them.
Code:
public void leave() {
     <musicname>.stop();
}
This will tell us to stop playing the music when we aren't in the state. This isn't the best, but hopefully I kinda explained it to you.


No errors, but the music doesn't seem to play.
I have the music initialized in my variable list like so
Code:
Music openingMenuMusic;


I then have this in the init method
Code:
openingMenuMusic = new Music("res//music//through_the_valleys.ogg");


and then the 2 methods you've suggests done almost identically.
Am I not importing the music correctly?


Top
 Profile  
 
PostPosted: Thu Aug 16, 2012 1:55 pm 
Offline

Joined: Sun Jul 08, 2012 10:33 am
Posts: 27
try this

Code:
   public void enter(GameContainer gc, StateBasedGame sbg) {
      backgroundMusic.play();   
      backgroundMusic.loop();
   }
   
   public void leave(GameContainer gc, StateBasedGame sbg) {
      backgroundMusic.stop();
   }


Top
 Profile  
 
PostPosted: Thu Aug 16, 2012 8:30 pm 
Offline

Joined: Sat Aug 11, 2012 2:07 am
Posts: 18
Khorne wrote:
try this

Code:
   public void enter(GameContainer gc, StateBasedGame sbg) {
      backgroundMusic.play();   
      backgroundMusic.loop();
   }
   
   public void leave(GameContainer gc, StateBasedGame sbg) {
      backgroundMusic.stop();
   }

yeah, that's what I have.
It seems the methods just aren't being called at all for some reason.


Top
 Profile  
 
PostPosted: Thu Aug 16, 2012 9:51 pm 
Offline
Regular

Joined: Tue Aug 23, 2011 8:19 am
Posts: 111
Test whether they're being called by printing to the console in each of the 2 methods. Maybe they're getting called but you play the music somewhere else.

_________________
My indie games: http://cmagames.webs.com


Top
 Profile  
 
PostPosted: Fri Aug 17, 2012 1:29 am 
Offline

Joined: Sat Aug 11, 2012 2:07 am
Posts: 18
cmasupra wrote:
Test whether they're being called by printing to the console in each of the 2 methods. Maybe they're getting called but you play the music somewhere else.


Upon doing this I noticed the message is printed twice.
So I moved the chunk of code to a different state, and the music stops and starts like it should then.

That still puzzles me, though, as to why the statement was printed twice on the other state. The reason responsible for it seems to by why I can't have music on it.
Strange.


Top
 Profile  
 
PostPosted: Sat Aug 18, 2012 8:13 am 
Offline

Joined: Sun Jul 08, 2012 10:33 am
Posts: 27
guess its fault is your set up of the game. do you have added the states twice in the main-class or is there anything else what you call twice related to the states?

edit: its enough to add the state. you dont have to call the init method explicit.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group