question

BlitzPlus Forums/BlitzPlus Programming/question

ngi(Posted 2004) [#1]
hello , I am a newbie and I was wondering how it is possible to play only one sound at a time , for example (part of my code)

Function collide()
;tested & working , must add some more things tho , remember joaquin made this program ehhhhh!

If ImagesCollide (player\image , player\x,player\y,0,bullets\image,bullets\x,bullets\y,0)
player\score = 0
PlaySound (sound(Rand(1,8)))
EndIf

and everytime an image collides like 20 of those sounds come at the same time , this is inside my main loop , and I want to know how to play only one at a time , thanks!


asdfasdf(Posted 2004) [#2]
You could set a varible that says you have already played it.
ex.
Function collide()

If ImagesCollide (player\image , player\x,player\y,0,bullets\image,bullets\x,bullets\y,0) 
    player\score = 0 
    PlaySound (sound(Rand(1,8)))
    player\sound = 1 ;this means it has played the sound
EndIf 

Then when you restart (or when ever else you want to do it) you set player\sound to 0.
P.S. it does not have to be player\sound.
P.S. I'm a Blitz3D user just looking in the BlitzPlus forum so if it doesn't work I used a Blitz3D command


ngi(Posted 2004) [#3]
and how do I make it to play the sound again after? , I did it with an IF command and it looks like this

If ImagesCollide (player\image , player\x,player\y,0,bullets\image,bullets\x,bullets\y,0)
player\score = 0
If bullets\sound = 0
PlaySound (sound(Rand(1,8)))
bullets\sound = 1
EndIf
EndIf


WolRon(Posted 2004) [#4]
A better method is to use channels.

Assign your sound to a channel when you play it like this:
chnSound=PlaySound(Sound(Rand(1,8)))
This way, when your images collide, you can check to see if the sound is playing already. If it isn't playing the last sound any more, then play another one.
If Not ChannelPlaying(chnSound)
  chnSound=PlaySound(Sound(Rand(1,8)))[/
EndIf



aab(Posted 2004) [#5]
the same thing happened with me for removing hps, so i made a variable eg: m, and used it as follows:
------------
if images overlap(,,,,,), and m=0
hp=hp-10
playsound hurt_sound
m=20
endif

if m>0 m=m-1
------------
this allows exactly 10 hps to be removed, and only plays the sound once, assuming the images have finished crossing after m reaches 0