简体   繁体   中英

Adding “Monsters” to a TileMap - LIBGDX

Thanks to alot of help from diff people willing to kindly share their help I have been able to read tilemaps and add my player start to the tilemap

for (int x = 0; x < layer3.getWidth(); x++) {
    for (int y = 0; y < layer3.getHeight(); y++) {
        TiledMapTileLayer.Cell cell = layer3.getCell(x, y);
        if (cell == null)
            continue;
        if (cell.getTile() == null)
            continue;
        if (cell != null) {
            TiledMapTile tile = cell.getTile();
            if (tile != null) {
                if (layer3.getCell(x, y).getTile().getProperties()
                            .containsKey("Start"))
                            player.position.set(x, y);     

However I would like to also place my monsters on the tilemap like the player, with the exception of i have multiple places on the tilemap to spawn monsters. The below code will only allow me to spawn one monster

for (int x = 0; x < layer2.getWidth(); x++) {
    for (int y = 0; y < layer2.getHeight(); y++) {
        TiledMapTileLayer.Cell cell = layer2.getCell(x, y);
        if (cell == null)
            continue;
        if (cell.getTile() == null)
            continue;
        if (cell != null) {
            TiledMapTile tile = cell.getTile();
            if (tile != null) {
                if (layer2.getCell(x, y).getTile().getProperties()
                        .containsKey("monster"))
                        monsters.position.set(x,y);

How could I spawn multiple monsters instead of one? Thank you in advance!

Have a list of Monsters

Array<Monster> monsters = new Array<Monster> //libgdx Array

And instead of this:

monsters.position.set(x,y); //wrong!

add a new monster:

monsters.add(new Monster(x, y)); //right!

Of course, use the parameters x and y in your monster contructor to set its position.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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