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
  • 3139 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
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
February 12, 2012

I took out the line

'soundF();'  which you put after declaring the variables and the error SecurityError: Error #2000: No active security context doesn't show anymore but the sounds still don't play. a friend said the publish settings could be the problem however they're pretty much the same as I had them when i was using the code i originally posted here and the sounds played.

I unchecked export SWC though and the warning 'No libraries were linked as Runtime Shared Libraries (RSLs) because of your publish settings: Export SWC' also dissappeared.


what are the file names of your mp3's?