0

我正在尝试使用键盘移动生成的网格...当我尝试使用我的 .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;              
}

谢谢 !

4

1 回答 1

0

我现在看到几个问题:

  • paramMap并且它的内部函数movingGrid永远不会被任何人调用。您可能希望将它们连接到ENTER_FRAME事件侦听器或在用户按下键时调用它们。

  • 和属性不在这部分代码中,所以在我测试时它会抛出vit一个。对于.TgridReferenceErrorcheckLateral

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

var grid:MovieClip = new MovieClip();

addChild(grid);

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;


    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 +=  sens;
        L = y;
        C = x;
        Ls = (y - 1);
        Cs = (x + sens);
    }
}



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

    paramMap();
    trace(grid.x, grid.y);
}

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;
    }
}
于 2012-11-20T23:14:26.093 回答