Skip to main content
Participating Frequently
February 12, 2012
Question

How to control mouse events with other mouse events Actionscript 3, please help!

  • February 12, 2012
  • 1 reply
  • 3141 views

Hello,

I'm working on a virtual guitar simulation project and to be honest my flash skills are very beginner.

Right now all the sounds (78 of them) on my guitar play using the roll_over event but that doesn't give the user much control over what sound is played.

The actionscript im using to play my sounds is this;

for(var i:uint = 0; i < buttonArray.length; i++){

buttonArray.addEventListener(MouseEvent.ROLL_OVER, buttonRolledOver);

}

//This function stops any sound clip that is playing and

//plays the sound file thats clicked.

function buttonRolledOver(e:MouseEvent):void{

// SoundMixer.stopAll(); -- stops sounds playing when next one is played

for(var i:uint = 0; i < buttonArray.length; i++){

if(e.target == buttonArray){

var s:Sound = new Sound();

s.load(new URLRequest(soundArray));

s.play();

}

}

}

What i want to do is only play a sound with the roll_over event when the left click is held down.

I'd very much appreciate some help with that. A friend of mine advised me to use an if statement but I'm not really sure how to implement that.

Thanks!

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
February 12, 2012

first, you should initialize your app by loading all your sounds and store the sound objects in soundArray.  otherwise, that's a major memory hog and sound-start latency will be an issue if that's deployed on the internet.

also, you should be using movieclip buttons or simple buttons with names that make it easy to extract i from buttonArray.  then you can use:

var sc:SoundChannel;

for(var i:uint = 0; i < buttonArray.length; i++){

buttonArray.addEventListener(MouseEvent.MOUSE_DOWN, startF);

buttonArray.addEventListener(MouseEvent.MOUSE_UP, stopF);

}

//This function stops any sound clip that is playing and

//plays the sound file thats clicked.

function startF(e:MouseEvent):void{

sc=soundArray[extractIndexF(e.currentTarget.name)].play();

}

function stopF(e:MouseEvent):void{

sc.stop();

}

function extractIndexF(nameS:String):int{

// return index of buttonArray item

}

Participating Frequently
February 12, 2012

Thanks for your reply. Yes i'm using a soundarray which stores all the sounds, here is a snippet of it. I hope this is what you mean.

var soundArray:Array = new Array();

soundArray[0] = 'sounds/01.mp3';

soundArray[1] = 'sounds/1.mp3';

soundArray[2] = 'sounds/2.mp3';

soundArray[3] = 'sounds/3.mp3';

soundArray[4] = 'sounds/4.mp3';

soundArray[5] = 'sounds/5.mp3';

soundArray[6] = 'sounds/6.mp3';

soundArray[7] = 'sounds/7.mp3';

soundArray[8] = 'sounds/8.mp3';

soundArray[9] = 'sounds/9.mp3';

soundArray[10] = 'sounds/10.mp3';

soundArray[11] = 'sounds/11.mp3';

soundArray[12] = 'sounds/12.mp3';

and a code snippet of the button array:

var buttonArray:Array = new Array();

buttonArray[0] = lowe_btn;

buttonArray[1] = lowe1_btn;

buttonArray[2] = lowe2_btn;

buttonArray[3] = lowe3_btn;

buttonArray[4] = lowe4_btn;

buttonArray[5] = lowe5_btn;

buttonArray[6] = lowe6_btn;

buttonArray[7] = lowe7_btn;

buttonArray[8] = lowe8_btn;

buttonArray[9] = lowe9_btn;

buttonArray[10] = lowe10_btn;

buttonArray[11] = lowe11_btn;

buttonArray[12] = lowe12_btn;

Anyway I put that code you posted into my program to replace the code I posted here initially but it comes up with an error referring to that last function which only has a comment on it.

kglad
Community Expert
Community Expert
February 12, 2012

no, that's not an array of sounds.  your soundArray is an array of strings.

you should use something like:

var soundArray:Array = new Array();

var buttonArray:Array = new Array();

var index:int=0;

soundF();

function soundF():void{

var s:Sound=new Sound();

s.load(new URLRequest("sounds/"+index+".mp3"));  // i'm assuming 01.mp3 should be 0.mp3

s.addEventListener(Event.COMPLETE,loadcompleteF);

soundArray.push(s);

buttonArray.push(this["lowe"+index+"_btn"]); // rename lowe_btn to lowe0_btn

}

function loadcompleteF(e:Event):void{

buttonArray[index].addEventListener(MouseEvent.MOUSE_DOWN, startF);

buttonArray[index].addEventListener(MouseEvent.MOUSE_UP, stopF);

buttonArray[index].addEventListener(MouseEvent.MOUSE_OUT, stopF);

index++;
if(index<12){
soundF();
}
}


//This function stops any sound clip that is playing and

//plays the sound file thats clicked.

function startF(e:MouseEvent):void{

sc=soundArray[extractIndexF(e.currentTarget.name)].play();

}

function stopF(e:MouseEvent):void{

if(sc){

sc.stop();

}

}

function extractIndexF(nameS:String):int{

return int(nameS.split("lowe")[1].split("_")[0]);

}