Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How would I achieve this result? ActionScript 3

Mentor ,
Sep 23, 2013 Sep 23, 2013

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

TOPICS
ActionScript
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Sep 23, 2013 Sep 23, 2013

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.

Translate
LEGEND ,
Sep 23, 2013 Sep 23, 2013

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Sep 23, 2013 Sep 23, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Sep 23, 2013 Sep 23, 2013

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();

    }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 23, 2013 Sep 23, 2013

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Sep 23, 2013 Sep 23, 2013

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().

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Sep 23, 2013 Sep 23, 2013

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Sep 23, 2013 Sep 23, 2013
LATEST

I think I have it sorted out now.  many thanks for your input and wisdom.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines