A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: AS3 Overlapping sounds - Boolean dosen't work HELP!!!

  1. #1
    Member
    Join Date
    Jan 2003
    Location
    Denmark, copenhagen
    Posts
    55

    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

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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.

  3. #3
    Member
    Join Date
    Jan 2003
    Location
    Denmark, copenhagen
    Posts
    55
    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

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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.

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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.

  6. #6
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    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.CLICKalarmFunction);

    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;


  7. #7
    Member
    Join Date
    Jan 2003
    Location
    Denmark, copenhagen
    Posts
    55

    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center