For the narration, I decided to use a different SoundChannel and SoundTransform.
I added a script similar to that of the music, to add the sound effect.
I wanted the sound to play if the door was locked.
Here is the code I used originally:
| if(!doorUnlocked){ trace("Door is jammed shut"); narrateChannel=narrateItsLocked.play(0,0,narrateTrans); } |
This script was placed inside the button of the door.
The problem with the script was that every time the door was clicked the sound would play on top of itself.
I needed a way so that this would not happen.
At first I tried:
| if(narrateChannel!=narrateItsLocked){ narrateChannel=whatsThat.play(0,0,narrateTrans); } |
This was a good idea but it did not work.
I noticed that the channel would be null (hasn't been set to any sound) until a sound was played. After it's played the sound it was no longer set to null. Knowing this I tried the following script:
| if(!narrateChannel){ Channel=narrateItsLocked.play(0,0,narrateTrans); } else if (narrateChannel){ Channel=null; } |
This worked... but it wasn't to the extent I wanted.
My script made it so that it would only play once null. If it wasn't null, then it would make it null.
By doing so, it added a delay so that the script would only play the sound every other time you press it. Which meant that the sound would still overlap, it would take two clicks instead of one.
I then searched through the internet to find a script which acknowledge when the sound would stop playing. This was known as SOUND_COMPLETE.
I used the following script:
| if(!narrateChannel){ trace("1 narrateChannel "+narrateChannel); narrateChannel=narrateItsLocked.play(0,0,narrateTrans); trace("2 narrateChannel"+narrateChannel); } narrateChannel.addEventListener(Event.SOUND_COMPLETE,onSoundChannelSoundComplete); function onSoundChannelSoundComplete(e:Event):void{ trace("done"); narrateChannel=null; } |
This caused the following error:
Cannot access a property or method of a null object reference.
This meant that the script couldn't find when the sound was complete because it was on null.
I then adjusted the placement of the eventlistener, so it would not search for it straight away, but instead when the button was pressed.
My script now looked like this:
| if(!narrateChannel){ trace("1 narrateChannel "+narrateChannel); narrateChannel=narrateItsLocked.play(0,0,narrateTrans); trace("2 narrateChannel"+narrateChannel); narrateChannel.addEventListener(Event.SOUND_COMPLETE,onSoundChannelSoundComplete); } function onSoundChannelSoundComplete(e:Event):void{ trace("done"); narrateChannel=null; } |
This way the eventListener would be played only after the channel had been set.
The script worked perfectly.
I used these to help me add the sounds:
http://www.trainingtutorials101.com/2010/01/adjusting-volume-in-flash-actionscript.html
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/Sound.html#play%28%29
No comments:
Post a Comment
Note: only a member of this blog may post a comment.