添加影片剪辑在网格阶段基于关键presses [英] Adding MovieClips in a grid to stage based on keyPresses

查看:115
本文介绍了添加影片剪辑在网格阶段基于关键presses的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想看看是否有人能帮助我走出这个,我失去了我的心才!我需要添加的movieclip每个字母键的键盘上的(a到z)用户presses,就开始在阶段程序水平穿过阶段向右的左上角的阶段。 在舞台上键,开始于舞台的左上角和水平继续在舞台上为每个后续字符。目前,我有这个code然而被输入到一个容器重叠,因为用户$ P $的影片剪辑psses键盘上的按键。帮助急需!

Looking to see if anyone can help me out with this before i lose my mind! I am required to add movieclips for each of the alphabetical keys (a to z) the user presses on the keyboard, on the stage commencing at the top left corner of the stage proceeding horizontally across the stage towards the right. key on the stage, commencing at the top left corner of the stage and proceeding horizontally across the stage for each subsequent character. I currently have this code however the movieclips that are entered into a container overlap as the user presses the keys on the keyboard. Help desperately needed!

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

推荐答案

我可能不会跨$ P $正确pting你的问题,但它听起来像是当一个字母键是pssed键盘上的$ P $,你希望那信露面以分布式方式在屏幕上。在这种情况下,像这样的,除了工作,以您目前的code:

I may not be interpreting your question properly, but it sounds like whenever a letter key is pressed on the keyboard, you want that letter to show up on the screen in a distributed fashion. In which case, something like this would work in addition to your current code:

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

这篇关于添加影片剪辑在网格阶段基于关键presses的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆