Actionscript 3 基于按键将网格中的MovieClips添加到舞台

Actionscript 3 基于按键将网格中的MovieClips添加到舞台,actionscript-3,flash,Actionscript 3,Flash,在我失去理智之前看看是否有人能帮我解决这个问题!我需要为用户在键盘上按下的每个字母键(a到z)添加movieclips,从舞台左上角开始,水平穿过舞台向右移动。 按舞台上的键,从舞台的左上角开始,然后在舞台上水平移动,以供后续角色使用。我目前有这个代码,但是当用户按下键盘上的键时,输入到容器中的movieclips会重叠。急需帮助 import flash.display.MovieClip; var nextYPos:int = 25; var i:int = 0; var A_mc = n

在我失去理智之前看看是否有人能帮我解决这个问题!我需要为用户在键盘上按下的每个字母键(a到z)添加movieclips,从舞台左上角开始,水平穿过舞台向右移动。 按舞台上的键,从舞台的左上角开始,然后在舞台上水平移动,以供后续角色使用。我目前有这个代码,但是当用户按下键盘上的键时,输入到容器中的movieclips会重叠。急需帮助

import flash.display.MovieClip;
var nextYPos:int = 25;
var i:int = 0;

var A_mc = new AKey();
var B_mc = new BKey();
var C_mc = new CKey();
var D_mc = new DKey();
var E_mc = new EKey();
var F_mc = new FKey();
var G_mc = new GKey();//declare lose movieclip variable
var H_mc = new HKey();
var I_mc = new IKey();
var J_mc = new JKey();
var K_mc = new KKey();
var L_mc = new LKey();
var M_mc = new MKey();//declare lose movieclip variable
var N_mc = new NKey();
var O_mc = new OKey();
var P_mc = new PKey();
var Q_mc = new QKey();
var R_mc = new RKey();
var S_mc = new SKey();//declare lose movieclip variable
var T_mc = new TKey();
var U_mc = new UKey();
var V_mc = new VKey();
var W_mc = new WKey();
var X_mc = new XKey();
var Y_mc = new YKey();
var Z_mc = new ZKey();

var container:Sprite = new Sprite();
addChild(container);
container.x = 50;
container.y = 30;
//container.width = 30;

stage.addEventListener(KeyboardEvent.KEY_DOWN, letters);

function letters(e:KeyboardEvent):void{

    if (e.keyCode == Keyboard.A){
            container.addChild(A_mc);
            // A_mc = textinput;
            //A_mc.x = 50;//x value for lose movieclip addChild
            //A_mc.y = 30;//y value for lose movieclip addChild
             A_mc.x = nextYPos;

    }if (e.keyCode == Keyboard.B){
            container.addChild(B_mc);
            //B_mc = textinput;
            //B_mc.x = 75;
            //B_mc.y = 30;
             B_mc.x = nextYPos;


    }if (e.keyCode == Keyboard.C){
            container.addChild(C_mc);
            //C_mc.x = 100;
            //C_mc.y = 30;

    }if (e.keyCode == Keyboard.D){
            container.addChild(D_mc);
            //D_mc.x = 125;
            //D_mc.y = 30;

    }if (e.keyCode == Keyboard.E){
            container.addChild(E_mc);
            //E_mc.x = 150;
            //E_mc.y = 30;

    }if (e.keyCode == Keyboard.F){
            container.addChild(F_mc);
            //F_mc.x = 175;//x value for lose movieclip addChild
            //F_mc.y = 30;//y value for lose movieclip addChild

    }if (e.keyCode == Keyboard.G){
            container.addChild(G_mc);
            //G_mc.x = 195;
            //G_mc.y = 30;

    }if (e.keyCode == Keyboard.H){
            container.addChild(H_mc);
            //H_mc.x = 220;
            //H_mc.y = 30;

    }if (e.keyCode == Keyboard.I){
            container.addChild(I_mc);
            //I_mc.x = 245;
            //I_mc.y = 30;

    }if (e.keyCode == Keyboard.J){
            container.addChild(J_mc);
            //J_mc.x = 255;
            //J_mc.y = 30;

    }if (e.keyCode == Keyboard.K){
            container.addChild(K_mc);
            //K_mc.x = 275;//x value for lose movieclip addChild
            //K_mc.y = 30;//y value for lose movieclip addChild

    }if (e.keyCode == Keyboard.L){
            container.addChild(L_mc);
            //L_mc.x = 300;
            //L_mc.y = 30;

    }if (e.keyCode == Keyboard.M){
            container.addChild(M_mc);
            //M_mc.x = 320;
            //M_mc.y = 30;

    }if (e.keyCode == Keyboard.N){
            container.addChild(N_mc);
            //N_mc.x = 350;
            //N_mc.y = 30;

    }if (e.keyCode == Keyboard.O){
            container.addChild(O_mc);
            //O_mc.x = 375;
            //O_mc.y = 30;

    }if (e.keyCode == Keyboard.P){
            container.addChild(P_mc);
            //P_mc.x = 400;//x value for lose movieclip addChild
            //P_mc.y = 30;//y value for lose movieclip addChild

    }if (e.keyCode == Keyboard.Q){
            container.addChild(Q_mc);
            //Q_mc.x = 50;
            //Q_mc.y = 80;

    }if (e.keyCode == Keyboard.R){
            container.addChild(R_mc);
            //R_mc.x = 75;
            //R_mc.y = 80;

    }if (e.keyCode == Keyboard.S){
            container.addChild(S_mc);
            //S_mc.x = 100;
            //S_mc.y = 80;

    }if (e.keyCode == Keyboard.T){
            container.addChild(T_mc);
            //T_mc.x = 120;
            //T_mc.y = 80;

    }if (e.keyCode == Keyboard.U){
            container.addChild(U_mc);
            //U_mc.x = 140;//x value for lose movieclip addChild
            //U_mc.y = 80;//y value for lose movieclip addChild

    }if (e.keyCode == Keyboard.V){
            container.addChild(V_mc);
            //V_mc.x = 165;
            //V_mc.y = 80;

    }if (e.keyCode == Keyboard.W){
            container.addChild(W_mc);
            //W_mc.x = 190;
            //W_mc.y = 80;

    }if (e.keyCode == Keyboard.X){
            container.addChild(X_mc);
            //X_mc.x = 220;
            //X_mc.y = 80;

    }if (e.keyCode == Keyboard.Y){
            container.addChild(Y_mc);
            //Y_mc.x = 245;
            //Y_mc.y = 80;

    }if (e.keyCode == Keyboard.Z){
        container.addChild(Z_mc);
        //Z_mc.x = 270;
        //Z_mc.y = 80;

    }else {
    }
}

我可能无法正确解释您的问题,但听起来好像每当按下键盘上的字母键时,您都希望该字母以分布式方式显示在屏幕上。在这种情况下,除了您当前的代码之外,还可以执行类似的操作:

//any time something new is added to container, call distribute keys
container.addEventListener(Event.ADDED, distributeKeys);

function distributeKeys(e:Event):void {
    var padding:Number = 5; //padding between rows/columns
    var curY:Number = padding; //starting Y
    var curX:Number = padding; //starting X
    var curH:Number = 0; //the current rows tallest item (this is only needed if the height of your clips varies, if each row is a fixed height, then you can take out all references to curH.

    var curKey:DisplayObject; //for storing the current letter clip in the loop below

    //iterate through all the children of container
    for(var i:int=0;i<container.numChildren;i++){
        curKey = container.getChildAt(i);

        curKey.x = curX;

        //if curKey goes past the edge, move it down and left
        if(curKey.x + curKey.width + padding > stage.stageWidth){
            curX = padding; //reset the x coordinate to far left
            curKey.x = curX; //move curKey to the new current X spot

            //if using variable height (curH)
            curY += curH + padding; //increase the row position
            curH = 0; //reset the row height since we just started a new row

            //if using fixed height (every item has the same height) do this:
            // curY += curKey.height + padding;
        }

        curKey.y = curY; //make curKey y go to the current y position
        curX += curKey.width + padding; //increment the current X position
        curH = Math.max(curH, curKey.height); //store the current biggest item in this row, take out this line if using fixed height 
    }
}
//每当有新的内容添加到容器中时,调用distribute keys
container.addEventListener(Event.ADDED,distributeKeys);
函数分配键(e:事件):无效{
var padding:Number=5;//行/列之间的填充
var curY:Number=padding;//开始Y
var curX:Number=padding;//开始X
var curH:Number=0;//当前行最高的项(仅当剪辑的高度不同时才需要此项,如果每行的高度固定,则可以删除对curH的所有引用)。
var curKey:DisplayObject;//用于在下面的循环中存储当前字母片段
//遍历容器的所有子级
对于(变量i:int=0;i阶段宽度){
curX=padding;//将x坐标重置为最左侧
curKey.x=curX;//将curKey移动到新的当前x点
//如果使用可变高度(curH)
curY+=curH+padding;//增加行位置
curH=0;//重新设置行高,因为我们刚刚开始一个新行
//如果使用固定高度(每个项目的高度相同),请执行以下操作:
//curY+=curKey.height+填充;
}
curKey.y=curY;//使curKey y转到当前的y位置
curX+=curKey.width+padding;//增加当前X位置
curH=Math.max(curH,curKey.height);//存储此行中当前最大的项目,如果使用固定高度,则取出此行
}
}

我不确定我是否理解。您已经注释掉了所有x/y分配,因此所有按钮都将位于位置
0,0
。您是否想要在网格中分发所有电影剪辑的东西?这里有大量冗余代码,您可以轻松创建一个带有动态文本字段的库对象,并创建所有26个键在一个循环中。你不是想让别人帮你做作业,是吗?你上次的编辑看起来可疑地像是一个家庭作业。不,我不是想这么做,相信我。只是不能让那部分工作,我无法理解为什么。我在那里测试了它,非常感谢,真的帮助了我,让我回到了正轨我很乐意帮忙。请记住向上投有用的答案,并接受最合适的答案(绿色复选标记)-你从来没有接受过任何问题的答案,这在你的个人资料中反映不好,可能会让一些人不愿阅读/回答。是的,对不起,我不太擅长解释我想做的事情。。我上传了一个我的需求截图。我正在努力根据用户按键从左到右逐步将电影剪辑添加到舞台上。这就是我认为的,如果添加到现有代码中,代码应该可以工作。或者,您可以将my
distributeKeys
函数中的所有内容都放在
letters
函数的末尾。