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

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

问题描述

在我的编程课上,

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

在地图上,瓦片在数组中初始化为黑色,之后,每当歌曲的 leftpick 达到特定值时,它们就会随机变化.

<小时>

当瓦片改变时,我需要用新值更新数组.实际上,我总是收到错误 #1010:术语未定义且没有属性.

<小时>

这是瓷砖的初始化方式:

<块引用>

变量

 私有变量网格:MovieClip;私有变量 nbRow: int = 6;私有变量 nbCol: int = 12;私有变量 oneTiles:瓷砖;私有变量 t:瓷砖;公共变量 tMap: 数组 = [[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]];

<块引用>

创建地图

 私有函数 createGrid(): void {网格 = 新电影剪辑();添加孩子(网格);for (var r: int = 0; r 

<块引用>

设置瓷砖

 私有函数 displayTiles(): void {var i: int = 0;for (var r: int = 0; r 

<块引用>

这是每次左峰值到达时调用的函数

 私有函数 resetTiles(): void {for (var i: int = 0; i < grid.numChildren; i++) {oneTiles = grid.getChildAt(i) as Tiles;oneTiles.getTiles();//30% 几率 gotoAndStop(1) 黑色瓷砖//70% 的机会 gotoAndStop(2) 白色瓷砖}}

<块引用>

这是问题的根源:为了更新数组,我在resetTiles()的for循环中添加了这个.顺便说一句,我总是收到 错误 #1010:术语未定义且没有属性.:

 私有变量 posX: uint = 0;//全球的私有变量 posY: uint = 0;//全球的tMap[posX][posY] = oneTiles.getFrame();if(posX == 11 && posY != 5){posX = 0;posY++;}else if(posX == 11 && posY == 5){posX = 0;posY = 0;}别的{posX++;}跟踪(posX);跟踪(位置);}

<块引用>

  • 那么,问题出在哪里?通常,使用此代码,每次更改 tile 时,都会更新 tMap 中的好 tile.
  • 我做了一些测试,似乎问题的根源在于 tMap[posX][posY] = oneTiles.getFrame(); 不过,我可以'找出原因

公共函数 getFrame():void{this.currentFrame;}

解决方案

让我解释一下如何处理此类问题.一旦您了解到特定线路给您带来麻烦,您就需要了解整个情况.您应该了解所涉及的每个对象的当前状态和值,例如:

import flash.utils.getQualifiedClassName;函数 hyperTrace(prefix:String, target:*):void{痕迹("");跟踪(名称:",前缀);跟踪(值:",目标);跟踪(类型:",类型(目标));trace("Class:", getQualifiedClassName(target));}

然后你开始学习:

//预期结果:Tiles 类的实例hyperTrace("oneTiles", oneTiles);//预期结果:函数hyperTrace("oneTiles.getFrame", oneTiles.getFrame);//预期结果:数组hyperTrace("tMap", tMap);//预期结果:数组hyperTrace("tMap[" + posX + "]", tMap[posX]);//预期结果:inthyperTrace("tMap[" + posX + "][" + posY + "]", tMap[posX][posY]);

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

如果不深入挖掘,我猜要么 oneTiles 未定义,要么 posXposY 有错误的值,所以 >tMap[posX] 未定义.

In my programming class,

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.


When the tiles change, I need to update the Array with the new value. Actually, I always get an Error #1010: A term is undefined and has no properties.


This is how the tiles are initialised:

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

解决方案

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

And then you start learning:

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

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.

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天全站免登陆