How to change music on different backgrounds
Monkey Forums/Monkey Beginners/How to change music on different backgrounds
| ||
I was wondering get different background music on different backgrounds with the code I wrote below? I think I wrote the code below incorrectlyStrict Import mojo Class MyApp Extends App Field BackGround1: Bool = True Field BackGround2: Bool = False Method OnCreate:Int() SetUpdateRate(60) Return True End Method OnUpdate:Int() If BackGround1 Then PlayMusic("In_Game_Rock.wav") If KeyDown(KEY_ENTER) Then BackGround1= False BackGround2 = True End End If BackGround2 Then PlayMusic("DrumCorpsVideoMusic.wav") End Return True End Method OnRender:Int() Cls(80.80,80) Return True End End Function Main:Int() New MyApp() Return True End |
| ||
What actually happens? It looks okay... apart from your Cls... it has a period instead of a comma. Maybe try StopMusic() in your KeyDown If statement. |
| ||
If BackGround2 Then PlayMusic("DrumCorpsVideoMusic.wav") End To me, it seems like it's trying to start playing a music track every single frame, so 60 times a second in the above. If I were you I would have a method that changes the music based on a value. Field currentTrack:Int Method ChangeMusic:Void(newTrack:Int) currentTrack = newTrack Select newTrack Case 0: PlayMusic("In_Game_Rock.wav") Case 1: PlayMusic("DrumCorpsVideoMusic.wav") Default: Error "ChangeMusic() tried to play an invalid track : " + newTrack End End Method OnUpdate:Int() If KeyHit(KEY_ENTER) If currentTrack = 0 ChangeMusic(1) ElseIf currentTrack = 1 ChangeMusic(0) End End Return 0 End |
| ||
What you need to do is check MusicState() 0/1 which checks the Music Channel, then do something like.If MusicState()=0 then PlayMusic() Just be sure that when you do a track change that you StopMusic() the current track. |
| ||
I'd probably have something like: |
| ||
Thanks for the help. I got the methods that you all mentioned to work. Other than the music, I almost have a game up and running (after just a week of learning Monkey X). Thanks again. |
| ||
happy to be of some help, cant wait to see what your working on :) |