Search code examples
actionscript-3flashaudiosoundchannel

Flash AS3 - SoundChannel error #1009


I'm a bit of a Flash newb so apologies if this is a simple issue or fix. Anyways I'm having problems with some functions. I've noticed I'm getting error 1009 only on functions that attempt to stop a sound channel. Here's some of the code:

This is one the first frame of the first scene, just setting up my channels. All my sounds are done here too but I don't think those are a problem.

import flash.media.Sound;
import flash.media.SoundChannel;

var musicChannel:SoundChannel = new SoundChannel();
var speechChannel:SoundChannel = new SoundChannel();
var sfxChannel:SoundChannel = new SoundChannel();

And here's an example of one of the buttons that causes the 1009 error:

function goHome(e:MouseEvent):void {
speechChannel.stop();
sfxChannel.stop();
musicChannel.stop();
gotoAndPlay(2, "TitleScreen")
}

home.addEventListener(MouseEvent.CLICK, goHome);

Now what really confuses me is that this same code ran perfectly last night on my computer. I went to test my .swf on the computers at my college and my animation froze, so I checked in Flash and this is what I got.

I'm also getting the same 1009 error on some TweenMax stuff. And yes I did import everything necessary and all files that are required for these functions to run are in the root directory of the .fla file. I know this because, as with the other error, this stuff worked on my computer at home perfectly. I am running the exact same .fla file and code file I saved on my computer and I do have the 'com' folder and 'greenstock' swc files in the same directory as my .fla, just like on my PC, yet I'm stilling seeing 1009 with this:

import com.greensock.TweenLite;
import com.greensock.TweenMax;

TweenMax.to(musicChannel, 2, {volume:0});

Again this line is referencing a sound channel, so I'm 99% sure it's the root of these problems. I can swap out 'musicChannel' in this piece of code with 'Music1' (a sound declared in the first frame with the channels) and it stops the music fine, but whenever I try to stop sound channels it craps out on me.

Again sorry if this is really simple to fix but I'm quite confused because it was working fine last night. The Flash Player could be outdated but it's Adobe Flash Player 11 so I don't know if that would be the problem. Right now I'm at college so I don't know what version I have at home.

Thanks for your time.


Solution

  • SoundCahnnel can be null and when they are you can't try to tween their property. An easy way to deal with that is to check whether they are null or not:

    if(musicChannel)
    {
        TweenMax.to(musicChannel, 2, {volume:0});
    }
    

    Also SoundChannel objects are generated only when a Sound object play method is called so there's no need to instantiate them:

    var musicChannel:SoundChannel = new SoundChannel();//irrelevant piece of code
    var musicChannel:SoundChannel;//correct