Haxe Map仅检索`null' [英] Haxe Map only retrieves `null`

查看:63
本文介绍了Haxe Map仅检索`null'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在Haxe中使用地图。我正在尝试创建一个Tile对象的网格,并使用它们在网格上的索引作为键将它们添加到Map中。但是,当我尝试使用索引从地图中检索图块时,总是得到 null 的值。

I'm having trouble using Maps in Haxe. I'm trying to create a grid of Tile objects and add them to the Map using their index on the grid as a key. However, when I try to retrieve a Tile from the map using an index I always get a value of null.

有人可以解释为什么会这样吗?我以前从未使用过地图,也无法理解问题所在。我目前正在使用多维数组来获得相同的功能,但是地图似乎更方便。

Could someone explain why this is happening? I've never used a map before and I don't understand what the issue is. I'm currently using a multidimensional array to get the same functionality, but maps seem more convenient.

private function initTiles():Void
{
    var tempTile:Tile;
    tileMap = new Map();

    for (i in 0...widthTiles)
    {
        for (j in 0...heightTiles)
        {
            tempTile = new Tile(i * 32, j * 32);
            tileMap.set([i,j],tempTile);
        }
    }
}


推荐答案

问题是您实际上不是在创建多维数组,而是在创建键类型为 Array< Int> 的一维数组。如有疑问,可以使用 $ type(tileMap)让编译器告诉您它认为的类型。

The issue is that the you are not actually creating a multidimensional array, you are creating a single dimensional array where the key type is Array<Int>. If ever in doubt, you can use $type( tileMap ) to get the compiler to tell you what type it thinks you have.

在您的情况下,您将获得:

In your case, you would get:

Map<Array<Int>,Tile>; // This is an ObjectMap, where the object is an Array

当您真正想要的是:

Map<Int, Map<Int,Tile>>; // This is an IntMap, each value holding another IntMap

这是一个问题的原因使用以下行:

The reason this is an issue can be seen with this line:

trace( [0,0] == [0,0] ); // False!

基本上,在Haxe中,对象(包括数组)的相等性取决于它们是否是完全相同的对象,如果它们具有相同的值,则不是。在这种情况下,您正在比较两个不同的数组。即使它们具有相同的值,它们实际上是两个不同的对象,并且不相等。因此,它们无法为您的地图提供合适的键。

Basically, in Haxe equality of objects (including arrays) is based on if they are the exact same object, not if they hold the same values. In this case, you are comparing two different arrays. Even though they hold the same values, they are actually two different objects, and not equal. Therefore they don't make suitable keys for your map.

以下是您需要执行的工作示例:

Here is a working sample for what you need to do:

class Test {
    static function main() {
        initTiles();
        trace( tileMap[3][6] );
    }

    static var tileMap:Map<Int,Map<Int,Tile>>;
    static function initTiles():Void {
        var widthTiles = 10;
        var heightTiles = 10;
        tileMap = new Map();
        for (i in 0...widthTiles) {
            if ( tileMap[i]==null ) {
                // Add the sub-map for this column
                tileMap[i] = new Map();
            }
            for (j in 0...heightTiles) {
                // Add the tile for this column & row
                tileMap[i][j] = new Tile(i*32, j*32);
            }
        }
    }
}

class Tile {
    var x:Int;
    var y:Int;
    public function new(x:Int, y:Int) {
        this.x = x;
        this.y = y;
    }
}

并查看其实际效果: http://try.haxe.org/#E14D5 (打开浏览器控制台以查看跟踪)。

And to see it in action: http://try.haxe.org/#E14D5 (Open your browser console to see the trace).

这篇关于Haxe Map仅检索`null'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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