; Sound channel tracker Type ActiveChannel ; Channel containing the sound playing Field ChannelHandle ; Reference to the sound being played ; (each sound has a different address) Field SoundHandle ; Time when started playing Field Created ; How long to wait before being able to play same sound. Field TimeOut End Type ;------------------------------------------------------------------------------- ;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ; # Play sample with overlapping prevention ;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ; Play a sound from SoundHandle, but only if it is not already playing, ; depending on a custom overlapping time, defined by TimeOut. ; - If TimeOut equals 0, the new sound to play will be allowed to overlap. ; - Channels are automatically updated when calling PlaySample, ; regardless of how many times you call it or how long you wait. ;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Function PlaySample ( SoundHandle , TimeOut = False ) Local FindChannel.ActiveChannel Local FoundChannel.ActiveChannel Local Playing = False Local PlayNew = False ; Check all previously playing sounds/channels For FindChannel = Each ActiveChannel ; If this channel is still playing If ChannelPlaying ( FindChannel\ChannelHandle ) ; And the sound being played is identical to the new sound to play. If FindChannel\SoundHandle = SoundHandle ; Remember that it is already being played Playing = True ; And store it for later access FoundChannel = FindChannel End If ; This channel is no longer playing Else ; Remove it from the list Delete FindChannel End If Next ; The new sound to play is already playing (it was found in the list) If Playing ; If the time since creation exceeds the channel's overlap (time out) If MilliSecs () - FoundChannel\Created >= FoundChannel\TimeOut ; Playing the new sound is allowed (overlapping) PlayNew = True End If ; The new sound to play is not yet playing Else ; So playing the new sound is allowed (without hesitation) PlayNew = True End If ; Allowed to play new sound If PlayNew ; Create an object to track the channel FoundChannel = New ActiveChannel ; Remember the time of creation FoundChannel\Created = MilliSecs () ; Record the overlapping time FoundChannel\TimeOut = TimeOut ; Also remember which sound is used FoundChannel\SoundHandle = SoundHandle ; Play the sound (and keep track) FoundChannel\ChannelHandle = PlaySound ( SoundHandle ) ; Finally return the channel, ; so you can optionally modify its properties. Return FoundChannel\ChannelHandle End If End Function ;------------------------------------------------------------------------------- Local sound = LoadSound ( "Spawn.WAV" ) If Not sound Then RuntimeError "Unable to load sound" Repeat ; press space to play sound with overlap protection If KeyDown ( 57 ) PlaySample sound , 500 End If Until KeyHit ( 1 ) End