I have a question about music

Blitz3D Forums/Blitz3D Beginners Area/I have a question about music

En929(Posted 2011) [#1]
I have a question about music in the 2d BlitzBasic forum. I was wondering how do I make it so that the music is changed each time my character collides with a door. The code with details on what I'm trying to do is in the link below:

http://www.blitzbasic.com/Community/posts.php?topic=95491#1100139


John Blackledge(Posted 2011) [#2]
Well, you've got LoadSound.
Now look up the other sound calls in the help files within the Blitz editor - there are examples there of how to play sounds.


En929(Posted 2011) [#3]
I did, but it doesn't explain how to change the music when an item collides with another item that's written within the game loop. I tried everything and couldn't get my music to stop and change to a different song on each collision.

Last edited 2011


Kryzon(Posted 2011) [#4]
You have declared two different types for similar objects:
Type DOOR
	Field x,y
End Type 

Type DOOR2
	Field x,y
End Type 
Why not declare a single, generic 'door' type and create two instances of it? you don't need to know which door collided if you keep a handle to the loaded music in the types themselves:
Global MusicChannel%

Type TDoor
	Field x%, y%
	Field song
End Type

Door1.TDoor = New TDoor
Door1\x = [...] : Door1\y = [...]
Door1\song = LoadSound( ... )

Door2.TDoor = New TDoor 
Door2\x = [...] : Door2\y = [...]
Door2\song = LoadSound( ... )

;Two instances of the same type, but the variable holding the objects is named differently.


This way, your main loop door collision code can be:
For d.TDoor = Each TDoor
	;If the player collides with any door, doesn't matter which.
	If ImagesCollide( playerImage, pX, pY, frame, doorImage, d\x, d\y, 0) Then 
		StopChannel MusicChannel ;Stop whatever might be previously playing (even if 'nothing at all').
		MusicChannel = PlaySound d\song ;Play the door's music\sound.
	EndIf
Next

Much simpler, isn't it?
If you want a real challenge, try doing a fade-out and a fade-in in the volumes for the song already playing and the song to be played, respectively. This will require you storing their state (that is, 'playing' or 'not playing') so that you can make the appropriate changes.
Doing a fade-out\fade-in is much more natural than that 'snap' changing of song - which might even lead to cracks due to suddenly stopping a playing sound.

Last edited 2011


En929(Posted 2011) [#5]
Kryson, I know this is very very late. But, I just wanted to say thanks for the advice about having a single type for 2 instances.