LoadMovieclips Array单击Btn Array

时间:2014-11-30 05:13:59

标签: actionscript-3

如何点击任何按钮将数组加载到数组的具体内容Teddy I Grateful(加载Movieclips Array Click按钮数组)     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;
}}

2 个答案:

答案 0 :(得分:1)

如果 [家庭,关于,产品,服务,联系] , 和 [Btnhome,Btnabout,Btnproducts,Btnservices,Btncontact] 是对象实例名称,然后你的代码是错误的。

但是在这种情况下,您可以将它们定位一次,然后每次都将它们设置为可见/不可见;

// set as invisible
myArray[i].visible = false;
// set as visible
myArray[i].visible = true;

否则,如果每次都必须重新创建它们,那么必须为对象指定一个Object Class,并用它们的对象类调用它们

var mc = new myObjClass();
  1. 要提供可以从代码中引用的对象类名称,请执行以下操作;
  2. 图书馆面板中,
  3. 选择要创建实例的MovieClip,
  4. [右键单击]>属性...
  5. 检查导出for ActionScript
  6. 并在下面提供 Class 名称。然后在代码中使用该类名,每次都为它创建新的MovieClip实例。
  7. 根据您的Flash版本,该过程可能会略有不同。但无论如何你都可以谷歌。

    希望有所帮助。

答案 1 :(得分:0)

好的,我会再次尝试帮助你。但是你必须和那些试图帮助你的人交谈。如果你不明白只是问。此外,至少向上投票对您的问题有用的答案或标记为正确如果它可以正常工作。否则,没有人想要输入教程(因为有Google和ActionScript手册)。

测试我显示的代码的最佳方法是:制作新的空白FLA文件并保存到某个文件夹。现在转到属性(ctrl + F3)并在 Class:框中输入 Array_buttons_v1 (按回车并再次保存FLA)。现在,您可以点击类:框旁边的铅笔图标来编辑您的班级文档。您将使用下面显示的自动代码替换..(在照片中: MyClass 变为 Array_buttons_v1

image borrowed from: Adobe.com 借鉴的图像:Adobe.com

你的库中还需要5个movieClip(ctrl + L)。这是一个用作按钮MC的movieClip,然后在单击时在舞台上添加另外四个movieClip。必须右键单击库中的每一个并选择“属性”,然后在链接部分勾选导出for Actionscript 并在中使用以下名称盒子里面显示..

  1. 作为按钮使用的一个小MC: btn_MC (稍后我们将MC点击为真实按钮)
  2. 点击按钮时要添加的其他四个MC: MC1 MC2 MC3 MC4
  3. 现在您可以使用主要属性(crtl + F3)单击铅笔图标(请参阅照片中的铅笔)并删除所有自动代码并将此代码粘贴到那里..(尝试了解代码在做什么,而不仅仅是复制粘贴+ ..)。它将MC类名称 btn_MC 中的四个按钮放入某个阵列,然后将其他四个MC加载到阵列,然后从Mc阵列加载到屏幕上。希望它有所帮助

    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..
            }
    
        }
    
    }