Reputation:
How can click on any button to load the array into the concrete contents of the array Teddy I Grateful (Load Movieclips Array Click button Array)
var teddy:Array = [home,about,products,services,contact];
var l:int = teddy.length;
for (var j:int = 0; j < l; j++) {
var mc1=new teddy[j];
var mc2=new teddy[1];
teddy[j].buttonMode = true;
var Btn:Array = [Btnhome,Btnabout,Btnproducts,Btnservices,Btncontact];
var W:int = Btn.length;
for (var i:int = 0; i < W; i++) {
var mc:MovieClip = new Btn[i];
mc.buttonMode = true;
mc.x=400+i*100;
mc.y=600+i;
mc.addEventListener(MouseEvent.CLICK, clickHandler);
addChild(mc);
}
}
function clickHandler(event:MouseEvent):void {
switch (event.currentTarget) {
case mc :
addChild(mc1);
trace("home");
mc1.x=400;
mc1.y=200;
break;
case mc :
addChild(mc2);
trace("about");
mc2.x=400
mc2.y=300
break;
case products_mc :
trace("products");
break;
case services_mc :
trace("services");
break;
case contact_mc :
trace("contact");
break;
}}
Upvotes: 0
Views: 180
Reputation: 15881
Okay I will try to help you one more time. But you must talk to the people trying to help you. If you dont understand just ask. Also at least up-vote useful answers to your questions or mark as correct if it works okay. Otherwise no-one wants to type tutorials (because there is Google and ActionScript manual for that).
The best way to test my shown code is... Make a new blank FLA file and save to some folder. Now go to Properties (ctrl+F3) and in the Class: box type in there Array_buttons_v1 (press enter and save FLA again). Now you can click the pencil icon next to Class: box to edit your Class document. You will replace that auto-code with mine shown below.. (in photo: MyClass becomes Array_buttons_v1)
image borrowed from: Adobe.com
You will also need 5 movieClips in your library (ctrl+L). That is one movieClip to use as button MC and then four other movieClips to be added on stage when you click. Each one in Library must be right-clicked and choose "properties" then in Linkage section tick Export for Actionscript and use the following name in Class box shown there..
Now you can use the main Properties (crtl+F3) to click the pencil icon (see pencil in photo) and delete all that automatic code and paste this code there.. (try to understand what code is doing, not just copy+paste..). It makes four buttons from MC class name btn_MC into some array and then loads other four MCs to array then from Mc array can add to screen too. Hope it helps
package
{
//** You will need more IMPORTS as you use other Flash features (APIs)
//** But these two is enough here.. check AS3 manual or tutorials for what to add
import flash.display.MovieClip;
import flash.events.*;
public class Array_buttons_v1 extends MovieClip
{
public var MC_Array:Array = []; //new array is empty
public var Btn_Array:Array = [];
public var MC_0 : MC1 = new MC1();
public var MC_1 : MC2 = new MC2();
public var MC_2 : MC3 = new MC3();
public var MC_3 : MC4 = new MC4();
public var btn_0 : btn_MC = new btn_MC();
public var btn_1 : btn_MC = new btn_MC();
public var btn_2 : btn_MC = new btn_MC();
public var btn_3 : btn_MC = new btn_MC();
public var mClips_holder : MovieClip = new MovieClip;
public var buttons_holder : MovieClip = new MovieClip;
public function Array_buttons_v1()
{
//** Update MC array.. items are counted from 0,1,2,3 (NOT 1,2,3,4)
MC_Array = [ MC_0, MC_1, MC_2, MC_3 ]; //meaning array pos [ 0, 1, 2, 3 ]
stage.addChild( mClips_holder ); //will hold
mClips_holder.y = 70; //move down so its not blocking anything
//** Update Buttons array
Btn_Array = [ btn_0, btn_1, btn_2, btn_3 ];
stage.addChild( buttons_holder ); //put Button holder on stage
//buttons_holder.addChild( Btn_Array [0] ); //put Buttons inside holder
var insert_pos:int = 0; //will use as screen "adding position" for buttons
//** To add all in array.. we start from pos of 0 and count up to 3
//** For every count number we do instructions inside { } until count finished
for (var arr_pos:int = 0; arr_pos <= 3; arr_pos++) //create some counter
{
trace("position inside the array is now : " + arr_pos);
//** setup instance Names for movieClips inside the MC Array
//** later you can access each one by name using "getChildByName" as MC_1 or MC_2 etc..
MC_Array[arr_pos].name = "MC_" + ( String(arr_pos) );
//** setup Buttons (names, clicked functions etc )..
Btn_Array[arr_pos].name = "button_" + ( String(arr_pos) );
Btn_Array[arr_pos].buttonMode = true; //make clickable before adding to screen
Btn_Array[arr_pos].addEventListener(MouseEvent.CLICK, button_Clicked);
buttons_holder.addChildAt( Btn_Array [arr_pos], arr_pos ); //add to container
buttons_holder.getChildAt(arr_pos).x = insert_pos;
trace("pos of btn is now : " + buttons_holder.getChildAt(arr_pos).x);
//update the adding position amount
insert_pos += 50; //add +50 pixels distance for next item
} //end For loop
}
public function button_Clicked ( evt :MouseEvent ) : void
{
//** Use "evt" because it matches with above "evt:MouseEvent" for access
trace( "Button name: " + evt.currentTarget.name + " ..was clicked" );
//** Now you can use IF statement to run another function(s)
//if (evt.currentTarget.name == "button_0") { some_Test(); }
//** or Use SWITCH statement (better and easier)
switch (evt.currentTarget.name)
{
case "button_0" :
//mClips_holder.addChild(MC_0); //** can be done like this ..
mClips_holder.addChild( MC_Array[0] ); //but you wanted from array so do this way..
mClips_holder.getChildByName("MC_0").x = 0;
some_Test(); //to do some other function
//** to stop this button listening for mouse clicked
//Btn_Array[0].removeEventListener(MouseEvent.CLICK, button_Clicked);
break;
case "button_1" :
mClips_holder.addChild(MC_1);
mClips_holder.getChildByName("MC_1").x = 40;
//Btn_Array[1].removeEventListener(MouseEvent.CLICK, button_Clicked);
break;
case "button_2" :
mClips_holder.addChild(MC_2);
mClips_holder.getChildByName("MC_2").x = 80;
//Btn_Array[2].removeEventListener(MouseEvent.CLICK, button_Clicked);
break;
case "button_3" :
mClips_holder.addChild(MC_3);
mClips_holder.getChildByName("MC_3").x = 120;
break;
} //end Switch/Case
}
public function some_Test ( ) : void
{
trace(" ### This is some other function... do extra things in this section");
//your extra code here..
}
}
}
Upvotes: 0
Reputation: 3190
if [home,about,products,services,contact], and [Btnhome,Btnabout,Btnproducts,Btnservices,Btncontact] are object instance Name's, then your code is wrong.
But in that case, you can position them once, then each time set them visible/invisible with this;
// set as invisible
myArray[i].visible = false;
// set as visible
myArray[i].visible = true;
Else, if you have to re-create them each time, then you must specify an Object Class for your objects, and call them with their object classes
var mc = new myObjClass();
Depending on your Flash version that process can be slightly different. But you can google it anyway.
Hope that helps.
Upvotes: 1