PDA

View Full Version : Sound control help


originalp
02-07-2011, 03:48 PM
Hello :D,

I'm trying to control sounds in my Flash with as3. I'm new to as3. Please help!

CLASS
I'm using a class with just a global boolean variable to trigger all sounds on or off:
package {

public class videosglobal {
public static var soundoff:Boolean;
}
}

Sound on/off buttons

import videosglobal
soundoff_btn.addEventListener(MouseEvent.CLICK,cli ccato);
//Note: "cliccato" is the name of the function, only here it appears with a space I don't know why, nevermind that.

function cliccato(m:MouseEvent):void
{
//SoundMixer.stopAll();
soundoff_btn.alpha=0;
soundon_btn.alpha=1;
//nevermind the alpha, it's just to make one or the other button visible.
videosglobal.soundoff=true;
//BOOLEAN GLOBAL VARIABLE IN CLASS SHOULD BE TRUE NOW.
}

soundon_btn.addEventListener(MouseEvent.CLICK,clic ccato2);
//Note: "cliccato2" is the name of the function, only here it appears with a space I don't know why, nevermind that.

function cliccato2(m:MouseEvent):void
{
//stopAllSounds Behavior
soundoff_btn.alpha=1;
soundon_btn.alpha=0;
//nevermind the alpha, it's just to make one or the other button visible.
videosglobal.soundoff=false;
//BOOLEAN GLOBAL VARIABLE IN CLASS SHOULD BE FALSE NOW.
}

MOVIE CLIPS
Now, I have different movieclips with sounds on their own timeline so I thought I could just ask for the global variable on the first frame of every sound in my Flash to mute all sounds if the sound on/off buttons are pressed (that's the whole idea!) :

import videosglobal
n=new Sound();
n.attachSound("airplane_sound");
if (videosglobal.soundoff==true){
n.stop();
}
else {
n.start(0, 1);
}
;

ERRORS

"Access of undefined property n."
But I don't even know if I'm doing the global boolean variable in a class right!
Please help!!
:confused:

SDragon029
02-07-2011, 05:05 PM
Flash can't determine what "n" is so that's why you got the error message. In AS3 all variables for the first time have to be defined like this "var n:Sound = new Sound();". You might want to also import "flash.media.Sound". Other than that I am not certain what to suggest.

Best of luck,
Tim

originalp
02-07-2011, 06:05 PM
Flash can't determine what "n" is so that's why you got the error message. In AS3 all variables for the first time have to be defined like this "var n:Sound = new Sound();". You might want to also import "flash.media.Sound". Other than that I am not certain what to suggest.

Best of luck,
Tim
I did it without declaring variable 'n' based on another thread (can't post link because of forum restrictions).[/url]

Anyway, I did what you suggested:
import videosglobal;
import flash.media.Sound;
var n:Sound=new Sound();
n.attachSound("airplane_sound");
if (videosglobal.soundoff==true){
n.stop();
}
else {
n.start(0, 1);
}
;

And got a new compiler error:
Call to a possibly undefined method attachSound through a reference with static type flash.media:Sound.

:confused:

SDragon029
02-07-2011, 08:13 PM
The attachSound method has been removed in AS3. Go to here: http://www.ultrashock.com/forum/viewthread/87090/ and read Nutrox's post it's helpful for what your looking for. If you have any questions on it feel free to ask and maybe I can shed some light on what portion your not understanding. There is also a post on this site about it located here: http://www.actionscript.org/forums/showthread.php3?t=181641

Cheers,
Tim