使用键盘从数组移动地图

时间:2012-11-20 21:36:32

标签: actionscript-3 flash


我想创建一个带有数组的地图......我在flash上​​看到了很多关于平台游戏创建的教程,但没有人在地图上移动。

所以,我试着自己做。 在移动地图之前,我要创建它,这就是我的问题! 所以,我让你看看三层代码并发表你的意见。

Flash告诉我一个错误#1009,他无法访问null对象引用的属性或方法而我看不到一次?

at Sansnom_fla::MainTimeline/createMap()
at Sansnom_fla::MainTimeline/frame1()

第1层

var grid:MovieClip = new MovieClip();   

// liste d'affichage
addChild(grid)                      

createMap()

第2层

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

第3层

 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]
                 ]

非常感谢! Ps:你可以学习,block是一个movieClip,其中一旦帧是一个特定的块。

1 个答案:

答案 0 :(得分:1)

为什么要在图层之间拆分代码?不要那样做。你还在第2层调用map之前它还存在(它不会在第3层代码运行之前创建)所以我的建议是将它全部粘贴到1层,在1帧。

然后调整您的代码顺序:

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();   

// liste d'affichage
addChild(grid);

createMap();

另外不要忘记;字符,你有一个错过它们的习惯。无论如何,希望这有帮助!