简体   繁体   English

Haxe - 为什么我不能在没有得到父级没有给定属性的错误的情况下访问子属性?

[英]Haxe - Why can I not access a child's attribute without getting an error that the parent does not have the given attribute?

I've recently been getting into Haxe and just started to use HaxeFlixel to load a Tiled .TMX file. 我最近进入Haxe并开始使用HaxeFlixel加载Tiled .TMX文件。

I am creating a TiledMap object and passing it the TMX file path, then I want to iterate over the layers in that object to add them to the game scene. 我正在创建一个TiledMap对象并将其传递给TMX文件路径,然后我想迭代该对象中的图层以将它们添加到游戏场景中。 However when I try to access .tileArray (which is a property of TiledTileLayer) I get the following error :- 但是,当我尝试访问.tileArray(这是TiledTileLayer的属性)时,我收到以下错误: -

flixel.addons.editors.tiled.TiledLayer has no field tileArray flixel.addons.editors.tiled.TiledLayer没有字段tileArray

Here is the code: 这是代码:

package;

import flixel.FlxState;
import flixel.tile.FlxTilemap;
import flixel.addons.editors.tiled.TiledMap;
import openfl.Assets;


class PlayState extends FlxState
{
    private var _tiled_map:TiledMap;

    override public function create():Void
    {
        _tiled_map = new TiledMap("assets/data/Map1.tmx");

        for(layer in _tiled_map.layers){
            var layerData:Array<Int> = layer.tileArray;
        }

        super.create();
    }

    override public function update(elapsed:Float):Void
    {
        super.update(elapsed);
    }
}

I've found the following example - http://coinflipstudios.com/devblog/?p=182 which seems to work fine for people. 我发现了以下示例 - http://coinflipstudios.com/devblog/?p=182 ,它似乎适用于人们。

So I wanted to check whether the layer object was a TiledTileLayer as it should be, or TiledLayer, with the following: 所以我想检查图层对象是否应该是TiledTileLayer,或TiledLayer,具体如下:

trace(Type.typeof(layer));

Which sure enough yields: 哪个肯定足够的产量:

PlayState.hx:24: TClass([class TiledTileLayer]) PlayState.hx:24:TClass([class TiledTileLayer])

So if it is a TiledTileLayer which has the field tileArray why is it moaning? 所以如果它是TiledTileLayer,它有字段tileArray为什么它在呻吟?

I had a look at the source code ( https://github.com/HaxeFlixel/flixel-addons/blob/dev/flixel/addons/editors/tiled/TiledMap.hx#L135 ) and TiledTileLayer inherits from TiledLayer. 我查看了源代码( https://github.com/HaxeFlixel/flixel-addons/blob/dev/flixel/addons/editors/tiled/TiledMap.hx#L135),TiledTileLayer继承自TiledLayer。 Layers is an array of type TiledLayer, so I think this is why it is moaning. 图层是TiledLayer类型的数组,所以我认为这就是为什么它在呻吟。 I can clearly see that the array is storing child objects of TiledLayer, but as soon as I access any props/methods of those children, it complains that the parent does not have that field? 我可以清楚地看到数组存储了TiledLayer的子对象,但是一旦我访问了那些子节点的任何道具/方法,就会抱怨父节点没有那个字段? Very confusing! 非常混淆!

To run I'm using this command: C:\\HaxeToolkit\\haxe\\haxelib.exe run lime test flash -debug -Dfdb 要运行我正在使用此命令:C:\\ HaxeToolkit \\ haxe \\ haxelib.exe运行lime test flash -debug -Dfdb

Thank you! 谢谢!

So if it is a TiledTileLayer which has the field tileArray why is it moaning? 所以如果它是TiledTileLayer,它有字段tileArray为什么它在呻吟?

It may be a TiledTileLayer in this case, but that may not always be the case. 这种情况下,它可能是TiledTileLayer ,但情况可能并非总是如此。 layers is an Array<TileLayer> after all, so it could be a TiledObjectLayer or a TiledImageLayer as well (which don't have a tileArray field). 毕竟, layers是一个Array<TileLayer> ,因此它也可以是TiledObjectLayerTiledImageLayer没有 tileArray字段)。 This can nicely be seen in the code you linked . 您链接的代码中可以很好地看到这一点。 The concrete type can only be known at runtime, but the error you get happens at compile-time. 具体类型只能在运行时知道,但是您在编译时遇到错误。

If you know for sure there won't be any object or image layers, you can just cast it to a TiledTileLayer . 如果您确定不会有任何对象或图像层,则可以将其TiledTileLayer转换为TiledTileLayer However, just to be safe, it's good practice to check the type beforehand anyway: 但是,为了安全起见,最好事先检查一下类型:

for (layer in _tiled_map.layers) {
    if (Std.is(layer, TiledTileLayer)) {
        var tileLayer:TiledTileLayer = cast layer;
        var layerData:Array<Int> = tileLayer.tileArray;
    }
}

It works without this for the tutorial you linked because it was made for an older version of flixel-addons. 它没有这个用于您链​​接的教程,因为它是为较旧版本的flixel-addons制作的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM