Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All TMXTiles in a TMXLayer need to be in the same TMXTileSet. #2

Open
albundyszabolcs opened this issue Oct 7, 2013 · 0 comments
Open

Comments

@albundyszabolcs
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant