AndEngineTMXTiledMapExtension icon indicating copy to clipboard operation
AndEngineTMXTiledMapExtension copied to clipboard

Multiple TMXLayer problem

Open albundyszabolcs opened this issue 10 years ago • 0 comments

Hi,

I am writing a game that uses TMX maps and tilesets. There is a tileset for the land (desert, water, hill, etc) and there is another tileset for the environment(trees, bushes, buildings, etc). The map has two layers. When I implemented this part and tried out, I got this exception: 10-07 23:00:55.598: E/AndroidRuntime(26679): java.lang.NullPointerException 10-07 23:00:55.598: E/AndroidRuntime(26679): at org.andengine.extension.tmx.TMXLayer.addTileByGlobalTileID(TMXLayer.java:277) 10-07 23:00:55.598: E/AndroidRuntime(26679): at org.andengine.extension.tmx.TMXLayer.initializeTMXTilesFromDataString(TMXLayer.java:247) 10-07 23:00:55.598: E/AndroidRuntime(26679): at org.andengine.extension.tmx.TMXParser.endElement(TMXParser.java:197) 10-07 23:00:55.598: E/AndroidRuntime(26679): at org.apache.harmony.xml.ExpatParser.endElement(ExpatParser.java:156)

if the pGlobalTileID is 0 this exception is always thrown. With a small reordering, this exception can be avoided:

private void addTileByGlobalTileID(final int pGlobalTileID, final ITMXTilePropertiesListener pTMXTilePropertyListener) { final TMXTiledMap tmxTiledMap = mTMXTiledMap;

    final int tilesHorizontal = mTileColumns;

    final int column = mTilesAdded % tilesHorizontal;
    final int row = mTilesAdded / tilesHorizontal;

    final TMXTile[][] tmxTiles = mTMXTiles;

    final ITextureRegion tmxTileTextureRegion;
    if (pGlobalTileID != 0) {
        tmxTileTextureRegion = tmxTiledMap
                .getTextureRegionFromGlobalTileID(pGlobalTileID);

        final int tileHeight = mTMXTiledMap.getTileHeight();
        final int tileWidth = mTMXTiledMap.getTileWidth();

        if (mTexture == null) {
            mTexture = tmxTileTextureRegion.getTexture();
            super.initBlendFunction(mTexture);
        } else {
            if (mTexture != tmxTileTextureRegion.getTexture()) {
                throw new AndEngineRuntimeException(
                        "All TMXTiles in a TMXLayer need to be in the same TMXTileSet.");
            }
        }
        final TMXTile tmxTile = new TMXTile(pGlobalTileID, column, row,
                tileWidth, tileHeight, tmxTileTextureRegion);
        tmxTiles[row][column] = tmxTile;

        setIndex(getSpriteBatchIndex(column, row));
        this.drawWithoutChecks(tmxTileTextureRegion, tmxTile.getTileX(),
                tmxTile.getTileY(), tileWidth, tileHeight,
                Color.WHITE_ABGR_PACKED_FLOAT);
        submit(); // TODO Doesn't need to be called here, but should rather
                    // be called in a "init" step, when parsing the XML is
                    // complete.

        /* Notify the ITMXTilePropertiesListener if it exists. */
        if (pTMXTilePropertyListener != null) {
            final TMXProperties<TMXTileProperty> tmxTileProperties = tmxTiledMap
                    .getTMXTileProperties(pGlobalTileID);
            if (tmxTileProperties != null) {
                pTMXTilePropertyListener.onTMXTileWithPropertiesCreated(
                        tmxTiledMap, this, tmxTile, tmxTileProperties);
            }
        }
    } else {
        tmxTileTextureRegion = null;
    }

    mTilesAdded++;
}

what is your opinion? Br

Szabolcs

albundyszabolcs avatar Jan 12 '15 12:01 albundyszabolcs