为什么当我更新二维数组时它给出 "Error #1010"

Why when I Update a 2 dimensional Array it give an "Error #1010"

在我的编程中class,

我需要用 AS3 创建一个 Tiles 游戏(比如 zelda)。

在地图上,方块在数组中初始化为黑色,之后,每当歌曲的左键达到某个值时,方块会随机变化。


当图块发生变化时,我需要用新值更新数组。实际上,我总是收到错误 #1010:术语未定义且没有属性。


瓷砖是这样初始化的:

variable

    private var grid: MovieClip;
    private var nbRow: int = 6; 
    private var nbCol: int = 12; 
    private var oneTiles: Tiles;
    private var t: Tiles;

    public var tMap: Array = [
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
    ];

Creation of the map

     private function createGrid(): void {
          grid = new MovieClip();
          addChild(grid);

          for (var r: int = 0; r < nbRow; r++) {
               for (var c: int = 0; c < nbCol; c++) {
                   t = new Tiles();
                   t.x = t.width * c;
                   t.y = t.height * r;
                   grid.addChild(t);
               }
         }
     }

Set Tiles

    private function displayTiles(): void {
        var i: int = 0;
        for (var r: int = 0; r < nbRow; r++) {
            for (var c: int = 0; c < nbCol; c++) {
                var t: Tiles;
                t = Tiles(grid.getChildAt(i));
                t.gotoAndStop(tMap[r][c]);
                i++;
            }
        }
    }

This is the function called everytime the leftpeak reach the value

    private function resetTiles(): void {
        for (var i: int = 0; i < grid.numChildren; i++) {
            oneTiles = grid.getChildAt(i) as Tiles;

            oneTiles.getTiles();
            //30% chance gotoAndStop(1) black tiles
            //70% chance gotoAndStop(2) white tiles
       }
    }

This is the source of the problem : to update the array, I added this in the for loop of resetTiles(). Whit it, I always get an Error #1010: A term is undefined and has no properties. :

            private var posX: uint = 0; //global
            private var posY: uint = 0; //global

            tMap[posX][posY] = oneTiles.getFrame();

            if(posX == 11 && posY != 5){
                posX = 0;
                posY++;
            }else if(posX == 11 && posY == 5){
                posX = 0;
                posY = 0;
            }else{
                posX++;
            }
            trace(posX);
            trace(posY);

        }
  • So, where's the problem ? Normaly, with this code, each time a tile is changed, the good tile in tMap shall be updated.
  • I did some test, and what seems to be the source of the problem is the line tMap[posX][posY] = oneTiles.getFrame(); Still, I can't figure out why
public function getFrame():void{
            this.currentFrame;
    }

让我解释一下如何处理这种问题。一旦您了解到某条线会给您带来麻烦,您就需要了解整个情况。您应该了解所涉及的每个对象的当前状态和值,例如:

import flash.utils.getQualifiedClassName;

function hyperTrace(prefix:String, target:*):void
{
    trace("");
    trace("Name:", prefix);
    trace("Value:", target);
    trace("Type:", typeof(target));
    trace("Class:", getQualifiedClassName(target));
}

然后你开始学习:

// Expected result: instance of Tiles class
hyperTrace("oneTiles", oneTiles);

// Expected result: Function
hyperTrace("oneTiles.getFrame", oneTiles.getFrame);

// Expected result: Array
hyperTrace("tMap", tMap);

// Expected result: Array
hyperTrace("tMap[" + posX + "]", tMap[posX]);

// Expected result: int
hyperTrace("tMap[" + posX + "][" + posY + "]", tMap[posX][posY]);

然后,您搜索与预期结果不匹配的那个。某些对象不是您在那里期望的(包括 posXposY 索引的值),这就是错误的来源。现在您了解了问题出在哪里,您应该弄清楚您之前的代码或开发逻辑到底出了什么问题,从而产生了意想不到的结果。

如果不深入挖掘,我猜 oneTiles 结果是未定义的,或者 posXposY 有错误的值,因此 tMap[posX] 未定义。