为什么当我更新二维数组时会给出“错误#1010”? [英] Why when I Update a 2 dimensional Array it give an "Error #1010"

查看:115
本文介绍了为什么当我更新二维数组时会给出“错误#1010”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的编程课上,

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

I need to create a Tiles game with AS3 (like zelda).

在地图上,磁贴被初始化为阵列中的黑色,然后,每当歌曲的左拨达到特定值时,它们就会随机改变。

On a map, the tiles are initialised black in an array and, after that, they are changing randomly each time the leftpick of a song reach a certain value.

这是平铺方式初始化:


变量

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




这是问题的根源:要更新阵列,我在此添加了resetTiles()的for循环。顺便说一句,我总是得到一个 Error#1010:一个术语是未定义的,没有属性。 :

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

        }





  • 那么,出什么问题了?通常,使用此代码,每次更改图块时,都应更新tMap中的好图块。

  • 我做了一些测试,问题的根源是第 tMap [posX] [posY] = oneTiles.getFrame(); 不过,我不知道为什么

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


推荐答案

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

Let me explain how to deal with this kind of problem. As soon as you learn that a specific line gives you trouble, you need to understand the whole picture. You should learn the current state and value of each object involved, something like that:

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

然后,搜索与预期结果不匹配的那个。某些对象不是您所期望的(包括 posX posY 索引的值),它是错误的根源。既然您了解了问题所在,就应该弄清楚以前的代码或开发逻辑到底出了什么问题,以便产生意想不到的结果。

Then, you search for the one that does not match the expected result. Some object is not what you expected there (including values of posX and posY indices) and that is the source of the error. Now that you learned where the problem is, you should figure what exactly went wrong with your previous code or development logic so that it produced the unexpected results.

无需深入研究可能会发现 oneTiles 尚未定义,或者 posX posY 的值有误,因此 tMap [posX] 未定义。

Without digging deep I'd guess that either oneTiles turned out to be undefined, or posX and posY has wrong values so tMap[posX] is undefined.

这篇关于为什么当我更新二维数组时会给出“错误#1010”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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