使用键盘移动生成的网格

时间:2012-11-20 22:45:51

标签: actionscript-3 flash

我正在尝试使用键盘移动生成的网格... 当我尝试我的.swf文件时,它运行但键盘... 这是一个像马里奥这样的平台游戏,所以你应该对那些没有感觉的代码行做好准备,但我向你保证会这样做!

所以,这是我的AS:

var map:Array = [
                 [1,1,1,1,1,1,10,0,0,11,1,1,1,1,1,1,1,1,1,1],
                 [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
                 [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
                 [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,0,1],
                 [1,0,0,0,0,0,6,1,0,0,4,4,4,4,0,0,6,1,0,1],
                 [1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
                 [1,0,0,0,1,6,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
                 [1,0,0,0,0,6,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
                 [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
                 [1,0,0,0,0,6,0,0,1,0,0,1,0,0,1,0,0,1,2,1],
                 [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
                 [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
                 [1,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
                 [1,1,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,1,3,1],
                 [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
                 ];

var stock:Array = [
             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
             [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
             ];

function createMap():void{
    for (var i:int=0; i<20; i++){                               
        for (var j:int=0; j<15; j++){                           

            var T:int = 32
            var dT:int = T*.5
            var f:int = map[j][i]

            if(f>0) {
                var t:Block = new Block()
                t.x= i*T;       
                t.y= j*T;       
                t.gotoAndStop(f)        
                grid.addChild(t)
            }
        }
    }
}

var grid:MovieClip = new MovieClip();   

addChild(grid);

createMap();

stage.addEventListener(KeyboardEvent.KEY_DOWN, pushin);
stage.addEventListener(KeyboardEvent.KEY_UP, pushout);      

var right:Boolean=false;
var left:Boolean=false;
var up:Boolean=false;
var down:Boolean=false;

var lateral:Boolean = false;            
var saute:Boolean = false ;             
var none:Boolean = true                 

function paramMap():void {

    grid.sens = int(right)-int(left);           
    grid.up = int(up)-int(down)             
    grid.jump = -int(up)*11                 
    none = !right && !left && !up && !down  
    lateral = right || left;                        



function movingGrid():void{

    var L:int                                                               
    var C:int                                                               
    var Ls:int                                                              
    var Cs:int                                                              
    var A:Object
    var B:Object
    var Y:Number
    var P:Array

    with(grid){

        x += vit*sens;

        L =  y/T;
        C =  x/T;
        Ls = (y+T-1)/T;         
        Cs = (x+vit*sens)/T;

        P =  checkLateral(grid,L,Cs,vit*2,(y-dT)/T)

        L =  P[0]
        Cs = P[1]
        A =  stock[L][C]
        B =  stock[Ls][C]
        Y =  (L-1)*T    

        y += gravite; 

        if (gravite++>T) gravite=T
    }
}


}

function pushin(event:KeyboardEvent):void {
    if (event.keyCode == 38) up = true; 
    if (event.keyCode == 40) down = true;
    if (event.keyCode == 39) right = true;
    if (event.keyCode == 37) left = true;
}

function pushout(event:KeyboardEvent):void {
    if (event.keyCode == 38) up = false;
    if (event.keyCode == 40) down = false;              
    if (event.keyCode == 39) right = false;             
    if (event.keyCode == 37) left = false;              
}

谢谢!

1 个答案:

答案 0 :(得分:0)

我现在看到几个问题:

  • paramMap及其内部函数movingGrid永远不会被任何人调用。您可能希望将它们挂钩到ENTER_FRAME事件侦听器,或者在用户按下某个键时调用它们。

  • vit的{​​{1}}和T属性不属于代码的这一部分,因此在测试时会抛出grid。同样适用于ReferenceError

解决这些问题后,网格对象会正确移动。但是因为它是一个空的动画片段,你实际上还不能看到它。您需要向其添加可见子项或移动现有对象。这是剥离的代码,正确地向左和向右移动checkLateral

grid