To get a sound file into your program, you first need to load it. After it is loaded, you can assign it to play at specific times. The following code will load a sound:
; Loading a sound sound = LoadSound (filename)
Obviously, you would replace filename with the actual name of your sound file. For example, in a video game with a blast, you might write:
blaster = LoadSound ("blaster.wav").
The sound part of the code is just a name we gave our sound; you can call your sounds anything you’d like.
Open the file called demo12-01.bb and save it to a new folder. Make sure that the file called blaster.wav is in the same folder. Follow along as we go through the code.
blaster = LoadSound ("blaster.wav")
The game that we have loaded contains a sphere and a cube, where the sphere can be controlled by moving the left and right arrow keys. Now that we have a sound loaded into the game, we need to create a certain event that will trigger the sound. In the case of this game, we will play the blaster sound whenever the sphere hits the cube.
While Not KeyDown(ESC_KEY) x#=0 y#=0 z#=0 If KeyDown(LEFT_KEY)=True Then x#=-0.05 If KeyDown(RIGHT_KEY)=True Then x#=0.05 If KeyDown(DOWN_KEY)=True Then y#=-0.05 If KeyDown(UP_KEY)=True Then y#=0.05 If KeyDown(Z_KEY)=True Then z#=-0.05 If KeyDown(A_KEY)=True Then z#=0.05 MoveEntity sphere,x#,y#,z# ; Collision Sound If CountCollisions (sphere) PlaySound blaster EndIf UpdateWorld RenderWorld Flip Wend
Let’s take a look at the code we created. We created an If statement that basically says that whenever a collision occurs involving the sphere, play the blaster sound. The code CountCollisions(sphere) says to Blitz3D, let me know if there have been any collisions involving the sphere, and tell me how many have taken place since the last UpdateWorld. The next piece of code, PlaySound blaster, tells Blitz3D to play the sound that we loaded earlier called blaster. Finally, the EndIf code ends the If statement.
3.135.201.52