Copy link to clipboard
Copied
Dear Forum members:
How would I go about achieving the following result?
I have a Movie Clip that is in my Flash library. It is essentially a button. There is a switch which enables the functionality that when the button is switched to an active loading state, a user can load a sound from a Flash window or from the file system (depending on how I design it). The switch gets turned off so that the sound stays locked to that button. The trick is that there are many buttons (let's say 16 to celebrate the Microsoft Surface 2 Music Kit).
I can manually create 16 event listeners but that seems like wasted code lines. Is there an automated way of adding a custom event listener to each of the 16 buttons that would load a different sound? Need help in methodology here. Thanks in advance, markerline
ButtonArray is a reference to an object in an array. It's not a loader. You'll have to create a loader object to do the loading. You should also create sound objects and load those sounds into the sound objects and then use the buttons to tell the sounds to play or stop.
Copy link to clipboard
Copied
1. Create an array to the references to the sound files.
2. Use a for... loop to create the instances of the button in the display list and
3. add the event listener for each button instance that references a single function
4. add each new button instance to a second array
5. In the function, use the currentTarget to find that button in the second array and then
6. load or play or stop the sound that is in the same position in the first array.
Copy link to clipboard
Copied
thank you. using arrays sounds like the right idea. just wasnt sure how to implement them. i will try that and let you know how i fare along.
sincerely, markerline
Copy link to clipboard
Copied
Here is some code:
I have an issue though. I am getting a 1006 error "load is not a function" on the line
buttonArray.load(new URLRequest(kicks_cg));
-------------------------
import flash.events.MouseEvent;
import flash.display.Loader;
import flash.net.URLRequest;
//hihats_ld 8
//kicks_cg 16
//kicks_ld 16
//misc_ld 16
//snares_cg 16
var kicks_cg:Array = new Array();
var kicks_ld:Array = new Array();
var snares_cg:Array = new Array();
var hihats_ld:Array = new Array();
var misc_ld:Array = new Array();
var kicks_cg_path:String=("native_instruments/kicks_cg/");
var kicks_ld_path:String=("native_instruments/kicks_ld/");
var snares_cg_path:String=("native_instruments/snares_cg/");
var hihats_ld_path:String=("native_instruments/hihats_ld/");
var misc_ld_path:String=("native_instruments/misc_ld/");
function make_kicks_cg_array():void{
//for(var i:int=1;i<17; i++){
for (var j:int=1;j<10;j++){
kicks_cg.push(kicks_cg_path+"kick_0"+j);
}
for (var jj:int=10;j<17;j++){
kicks_cg.push(kicks_cg_path+"kick_"+jj);
}
//}
}
function make_kicks_ld_array():void{
//for(var i:int=1;i<17; i++){
for (var j:int=1;j<10;j++){
kicks_ld.push(kicks_ld_path+"kick_0"+j);
}
for (var jj:int=10;jj<17;jj++){
kicks_ld.push(kicks_ld_path+"kick_"+jj);
}
//}
}
function make_snares_cg_array():void{
//for(var i:int=1;i<17; i++){
for (var j:int=1;j<10;j++){
snares_cg.push(snares_cg_path+"snare_0"+j);
}
for (var jj:int=10;jj<17;jj++){
snares_cg.push(snares_cg_path+"snare_"+jj);
}
//}
}
function make_hihats_ld_array():void{
for(var i:int=1;i<9; i++){
//for (var j:int=1;j<9;j++){
hihats_ld.push(hihats_ld_path+"hihat_0"+i);
//}
}
}
function make_misc_ld_array():void{
//for(var i:int=1;i<17; i++){
for (var j:int=1;j<10;j++){
misc_ld.push(misc_ld_path+"misc_0"+j);
}
for (var jj:int=10;jj<17;jj++){
misc_ld.push(misc_ld_path+"misc_"+jj);
}
//}
}
make_misc_ld_array();
make_hihats_ld_array();
make_kicks_cg_array();
make_kicks_ld_array();
make_snares_cg_array();
/*
trace(misc_ld);
trace(hihats_ld);
trace(kicks_cg);
trace(kicks_ld);
trace(snares_cg);
*/
var buttonArray:Array=new Array();
var buttonMC:button_mc=new button_mc();
function make_grid():void{
for(var i:int=0;i<16;i++){
for(var j:int=0;j<16;j++){
var buttonMC=new button_mc();
addChild(buttonMC);
buttonMC.x=buttonMC.width*i;
buttonMC.y=buttonMC.height*j;
buttonMC.buttonMode=true;
buttonMC.addEventListener(MouseEvent.CLICK, onButtonEvent);
buttonArray.push(buttonMC);
}
}
}
make_grid();
function onButtonEvent(e:MouseEvent):void{
//e.target.play();
for (var i=0;i<16;i++){
buttonArray.load(new URLRequest(kicks_cg));
e.currentTarget.play();
}
}
Copy link to clipboard
Copied
ButtonArray is a reference to an object in an array. It's not a loader. You'll have to create a loader object to do the loading. You should also create sound objects and load those sounds into the sound objects and then use the buttons to tell the sounds to play or stop.
Copy link to clipboard
Copied
Thanks. That worked perfectly. Not to mention I had to fix an error due to the silly fact that I forgot to append the mp3 extension in my arrays. I now have to figure out how to build out this application more so that I can incorporate hittestObject().
Copy link to clipboard
Copied
Let me second that thought. I have successfully managed to add a single sound to all 16x16 (256) buttons on the stage. However the reason I used the array to try to load the sounds before, and of course my syntax and procedure was wrong, was that I want a unique sound for all the buttons on the stage. Of course, I don't have 256 sounds but I have a lot of sounds for which I can reduce the size of my grid to accomodate the number of sounds that I have in my assets folder.
How do I attach a sound per array object?
Copy link to clipboard
Copied
I think I have it sorted out now. many thanks for your input and wisdom.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now