Play sound on collision problem?

Blitz3D Forums/Blitz3D Beginners Area/Play sound on collision problem?

NRJ(Posted 2016) [#1]
I am new to Blitz3D, I am trying to play a sound when the player collides with the door, using the entitycollided command the sound gets played but it is not clear, it looks like it is played again and again instead of playing just once, I am unable to solve this problem , anybody please help me out of this.


Bobysait(Posted 2016) [#2]
Use "ChannelPlaying(Sound)" to check if the sound is playing.
If not, play the sound

ps : Welcome there !


Dan(Posted 2016) [#3]
just earlier today i was trying to find some code for sound, for my project (before i write something for it).

this is what i found:

codearchive

and the post from topknot here is the origin of the above's link , i think.


Bobysait(Posted 2016) [#4]
If you want some stuff to use sounds, here is a little demo which manage to use multiple channels to play sounds.

I set it up to 4 channels arbitrary, but most hardware nowadays can maintain more than 30-50 channels at the same time.
It's just a sample test (old hardware are more restrictive, all depends on the target machine).

(I loaded a sound from windows but it may not exists on some version of windows... change the file for your needs)




ps : I have an old sound library, I thought I posted it long times ago ... seems I didn't :)


RemiD(Posted 2016) [#5]
I remember a problem that i have noticed in the past was that if there were 2 entities which emitted the same sound (which used the same channel) at the same time, only one entity emitted the sound. How to avoid that ? Maybe use one channel per entity (=different channel reference) ? Or load one sound per entity (=different sound reference) ?


NRJ(Posted 2016) [#6]
Still I am unable to solve this problem. I am just trying to play a sound like door locked when the player collides with the door(like in Clive barker, max Payne ) only single time but the sound is playing several times . The code I am using is below.

Graphics3d 800*600

Type_player=1
Type_door=2
--------------
--------------
Player=createcamera( )
--------------
--------------

EntityType player,type_player
Door=createcube( )
--------------
--------------
EntityType door,type_door

Collisions type_player,type_door,2,2
snd=loadsound("e:\sounds\door_locked.wav")


While not keyhit(1)

If entitycollided(player,type_door) then
Playsound snd

updateworld
Renderworld


Flip
Wend
End


Bobysait(Posted 2016) [#7]
well ...
I already answered your problem, everything is writen above.

assign a channel variable (init with 0) test if it exists and if so, if it's playing ...


local channel = 0

while not Keyhit(1)

[...]

if entitycollided(...)
    if channel=0 ; channel never used -> use it
        channel = playsound(snd)
    else ; channel exists -> check if it's playing.
        if not(channelplaying(channel)) then channel = playsound(snd)
    endif
endif

[...]
Wend

And that's all.


ps : you can use the [ code ] or [ codebox ] (without spaces) to surround your code.


skidracer(Posted 2016) [#8]
A simpler solution would be to add a new entity type

Type_door_closed =2
Type_door_open =3


Start cube as say door_closed and on collision set the entity type of the cube to door_open.

This solves the underlying problem that you only want a single collision event with a closed door.


NRJ(Posted 2016) [#9]
@BOBYSAIT
Thank you Bobysait for your help, your code has totally solved my problem, thanks for your consistent support.