-
AS3 Overlapping sounds - Boolean dosen't work HELP!!!
Hello everyone...long time since i was here last time...
anyway
I want to trigger a sound from my Library (linkage is: INM_Alarm)
I have a problem with the code below.
When i click the button a second time the sound should stop, but i get an error like this:
TypeError: Error #1009: Cannot access a property or method of a null object reference. at M_fla::MainTimeline/alarmFunction()[M_fla.MainTimeline::frame1:48]
___________________________________________
alarm_btn.addEventListener(MouseEvent.CLICK, alarmFunction);
var sndAlarm:SoundChannel;
var alarmPlaying:Boolean = true;
function alarmFunction(evt:MouseEvent):void
{
if(alarmPlaying)
{
var sndAlarm:INM_Alarm= new INM_Alarm();
sndAlarm.play();
}
else
{
sndAlarm.stop();
}
alarmPlaying = !alarmPlaying;
}
___________________________________________
How should my code look to prevent this problem???
Looking forward to your HELP..THANKS
-
Do not redeclare sndAlarm inside your function. When you do that, all references to sndAlarm in that function refer to the function-local variable of that name - even when the code path which would declare that variable is not exercised. Internally all variable declarations are effectively moved to the beginning of the function.
-
does this mean i just need to remove the:
alarmPlaying = !alarmPlaying;
If i'm wrong could you copy and paste my full code as it should be thanks
Regards
jimmy
-
No, and I have no idea how you got that from what I said.
Do not redeclare sndAlarm.
This is declaring:
Code:
var sndAlarm:SoundChannel;
This is redeclaring:
Code:
var sndAlarm:INM_Alarm = new INM_Alarm();
It is redeclaring because you are using the"var" keyword to declare a new variable with the same name in a new scope.
Remove "var" and ":INM_Alarm" from the line in the function.
-
Your alarmPlaying variable also has the wrong sense, but you reversed it again in the check so it should "work" as-is.
But your code now reads "if alarm is playing then play it. If it's NOT playing, then stop it". Fortunately, you lied about whether it starts out playing so the two errors cancel out.
-
besides what 5tons mentioned, there are other problems i think - play() is a method of the Sound object, but stop() is a method of the SoundChannel object. those need to be separated out - it appears sndAlarm is a Sound sometimes and a SoundChannel other times...
i'd probably do it something like this:
PHP Code:
alarm_btn.addEventListener(MouseEvent.CLICK, alarmFunction);
var sound:INM_Alarm = new INM_Alarm(); var channel:SoundChannel;
var alarmPlaying:Boolean = false;
function alarmFunction(evt:MouseEvent):void { if (alarmPlaying) { try { channel.stop(); } catch (e) { } } else { channel = sound.play(); } alarmPlaying = !alarmPlaying; }
-
Thanks
Thanks a million i'll try it tonight
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|