Warning: Undefined array key "keywords_en" in /mnt/web218/a3/28/510129628/htdocs/main.php on line 234 Warning: Undefined array key "description_en" in /mnt/web218/a3/28/510129628/htdocs/main.php on line 235 Warning: Undefined array key "commercials" in /mnt/web218/a3/28/510129628/htdocs/main.php on line 261 Tutorial 11

Tutorial 11

GL (very) Basic Tutorial

aka "so, you want to write a game, do you?"
© 2008 PeeJay

Lesson 11 - Breaking the Silence

Let's get some sounds in this game then! I have only used a couple of sounds to preserve bandwidth, but obviously your game could use something far more impressive!
First of all, let's load the files in, like this:
LOADSOUND "shot.wav",0,1
LOADSOUND "ow.wav",1,1
Like when loading the graphics, this will load sounds into memory ready to play. The first number is the identifier (note, this can be the same number that you have already used for graphics - GL Basic is clever enough to know the difference between the two!). The second number relates to how many times a sound can be played without being cut off - this is best kept as low as possible to preserve memory.
Having a look in the AddBullet function, we can now see the lines
	LOCAL soundpan=(px-320)/2000
	PLAYSOUND(0,soundpan,1)
have been added. That will obviously play the sound we have preloaded, but what is slightly less obvious are the numbers involved. Look at the help for the PLAYSOUND command, and you will see it has settings for pan and volume. Here, we are panning the sound from the left and right speaker dependent on the position of the player on the screen.
There is a new function - IsPlayerHit. Take a look - it is collision detection for our player this time - for each enemy it tests whether it is in collision with our player - if so, it will play the "ow" sound (using panning again) and delete the enemy. Note, we are also taking into account the image that is being used for our player, to ensure pixel perfect collision detection.

http://www.glbasic.com/files/tutorial/ImgLesson11.png
Well, that is nearly the end of this simple introduction to GL Basic - I hope you're enjoying it so far!
Download the Source Code and Media Files
Previous Lession
Next Lession