2
\$\begingroup\$

In the simple platform game I'm making, when player collects coin, its cell is set to null. Is it possible to recover all cells from layer "coins" after player dies? I have tried multiple things but none of them worked :/

Here is part of code responsible for collecting coins:

for (Rectangle coins : coins) {
        TiledMapTileLayer layer_coins = (TiledMapTileLayer)map.getLayers().get("coins");
        if(dotRect.overlaps(coins)){
            layer_coins.setCell((int)coins.x, (int)coins.y, null);
            coinsN++;
            Sounds.playCoin();
        }
    }

But when player dies and re-spawns, there are no coins if he collected them before. I tried reloading the map - it works fine, but loading after every death is unacceptable - it takes a lot of time.

EDIT: Now I have another problem, with loading TiledMapTile to put into cell...

TiledMapTile coinTile;

public void show() {
    TextureRegion mapTexture1Region = new TextureRegion(mapTexture1, 32, 0, 16, 16);
    coinTile.setTextureRegion(mapTexture1Region);
}

It gives me NullPointerException, and I have no idea why :/

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

You could have an array of coordinates of collected coins, and then iterate over that and restore them when the level should be restarted.

In general, for this problem you will want to keep the information around that you will still need later. You can do this either by remembering specific things explicitly or by cloning the original data.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks! I'll try that when I have a minute. I was thinking to do something like this, but I tought that maybe there is a simpler way - like saving the original layer for later, but this didn't really work for me :/ I'll accept your answer when I make sure everything works fine :) \$\endgroup\$
    – urban07
    Commented Apr 8, 2015 at 20:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .