为什么当我更新二维数组时会给出& quot;错误#10100& quot;错误消息. [英] Why when I Update a 2 dimensional Array it give an "Error #1010"

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

问题描述

在我的编程课上,

我需要使用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.

这是初始化图块的方式:

This is how the tiles are initialised:

变量

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

创建地图

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

设置图块

    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(); 还是,我可以找出原因

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.

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

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