How to control mouse events with other mouse events Actionscript 3, please help!
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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]);
}
Copy link to clipboard
Copied
oh right. i've tried to implement that code in replace of my array and my for loop but i get a compiler error saying 1120: access of undefined property sc. could you tell me what the problem is with that?
Copy link to clipboard
Copied
i omitted a needed line of code. use:
var sc:SoundChannel;
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]);
}
Copy link to clipboard
Copied
Hi thanks very much for your help so far. There are now no compiler errors now however I'm getting an error saying
"Warning: No libraries were linked as Runtime Shared Libraries (RSLs) because of your publish settings: Export SWC
SecurityError: Error #2000: No active security context."
and no sounds are playing
Sorry I'm not very competent with flash, I understand what some code does but when it comes to writing it myself I fail.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
what are the file names of your mp3's?
Copy link to clipboard
Copied
1, 2, 3, 4 and so on until 72. i know i should probably rename the 01, 02, 03, 04, 05, 06 files so the code runs for all the sounds in my project but i just wanted to test the code at least for sounds 1-12 which are all the sounds for the top string of my guitar except 1, which is 01.mp3 which is just the open note. here is an image of my sounds folder anyway.
Copy link to clipboard
Copied
use:
var sc:SoundChannel;
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"+indexF(index)+"_btn"]); // rename lowe_btn to lowe0_btn
}
function indexF(i:int):String{
if(i==0){
return "01";
} else {
return i.toString();
}
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]);
}
Copy link to clipboard
Copied
Hi, I used that code but unfortunately the sounds still don't work. I thought it might be my publish settings but they are no different from the publish settings of my old flash project and link to the right folder.
Copy link to clipboard
Copied
it looks like that should have been:
var sc:SoundChannel;
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/"+indexF(index)+".mp3")); // i'm assuming 01.mp3 should be 0.mp3
s.addEventListener(Event.COMPLETE,loadcompleteF);
soundArray.push(s);
buttonArray.push(this["lowe"+iindex+"_btn"]); // rename lowe_btn to lowe0_btn
}
function indexF(i:int):String{
if(i==0){
return "01";
} else {
return i.toString();
}
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]);
}
Copy link to clipboard
Copied
Hi, when I click on the buttons and rollover them I hear no sound. I tried that last code you've given me and nothing really changed.
Date: Tue, 14 Feb 2012 12:26:20 -0700
From: forums@adobe.com
To: jack_021@hotmail.co.uk
Subject: How to control mouse events with other mouse events Actionscript 3, please help!
Re: How to control mouse events with other mouse events Actionscript 3, please help!
created by kglad in Action Script 3 - View the full discussion
what doesn't work?
Replies to this message go to everyone subscribed to this thread, not directly to the person who posted the message. To post a reply, either reply to this email or visit the message page:
To unsubscribe from this thread, please visit the message page at . In the Actions box on the right, click the Stop Email Notifications link.
Start a new discussion in Action Script 3 by email or at Adobe Forums
For more information about maintaining your forum email notifications please go to http://forums.adobe.com/message/2936746#2936746.
Copy link to clipboard
Copied
i thought you wanted the sounds to play on mousedown, not on rollover.
Copy link to clipboard
Copied
I'd like the sounds to play on roll over but only when the left click is held down.
Date: Tue, 14 Feb 2012 13:39:33 -0700
From: forums@adobe.com
To: jack_021@hotmail.co.uk
Subject: How to control mouse events with other mouse events Actionscript 3, please help!
Re: How to control mouse events with other mouse events Actionscript 3, please help!
created by kglad in Action Script 3 - View the full discussion
i thought you wanted the sounds to play on mousedown, not on rollover.
Replies to this message go to everyone subscribed to this thread, not directly to the person who posted the message. To post a reply, either reply to this email or visit the message page:
To unsubscribe from this thread, please visit the message page at . In the Actions box on the right, click the Stop Email Notifications link.
Start a new discussion in Action Script 3 by email or at Adobe Forums
For more information about maintaining your forum email notifications please go to http://forums.adobe.com/message/2936746#2936746.
Copy link to clipboard
Copied
rollover is when the mouse rolls over the button. mousedown is when the left mouse button is depressed while over the button. drag over is when the left mouse button is depressed while off the button and then while the button is help down, the mouse is dragged over the button. is one or more of those, what you want?
and use the trace function to make sure your urlrequest url's match your file names.
Copy link to clipboard
Copied
i think something like this 'drag over is when the left mouse button is depressed while off the button and then while the button is help down, the mouse is dragged over the button.' id like the user to be able to left click anywhere and while its held down can drag over a button for the sound to play.
Copy link to clipboard
Copied
here's how to create dragover and dragout for an object b:
addStageListenersF();
function addStageListenersF():void{
stage.addEventListener(MouseEvent.MOUSE_DOWN,stagedownF);
stage.addEventListener(MouseEvent.MOUSE_UP,stageupF);
}
function stagedownF(e:MouseEvent):void{
b.addEventListener(MouseEvent.MOUSE_OVER,dragoverF);
b.addEventListener(MouseEvent.MOUSE_OUT,dragoffF);
}
function stageupF(e:MouseEvent):void{
b.removeEventListener(MouseEvent.MOUSE_OVER,dragoverF);
b.removeEventListener(MouseEvent.MOUSE_OUT,dragoffF);
}
function dragoverF(e:Event):void{
// play your sound
}
function dragoffF(e:Event):void{
// ? stop your sound
}
Copy link to clipboard
Copied
hello, can u make tutorial for this

