Use of the android game development framework libgdx (12)-Use of TiledMap

Source: Internet
Author: User
Tags libgdx

Tip: This article records my operation procedures and experiences. You may encounter some obstacles in your operations due to your version and other problems. Please refer to the comments and replies later in this article.

 

Although Image can be used as a background, it is a bit complicated to be an RPG Game. Libgdx SpriteCache can be used for efficiency purposes. However, libgdx also supports TiledMap.

Related Classes are TiledMap, TileAtlas, and TileMapRenderer, all of which are in com. badlogic. gdx. graphics. g2d. tiled.

Now let's take a look at the use of TiledMap from the beginning.

1. Create a TiledMap.

I am using Tile Map editor,: http://www.mapeditor.org/

Create a new map with a size of 50*30. The size of each block is 32*32.

The graph block resources I use are stored on the Internet, but the source is unknown. I would like to express my gratitude to the resource donor ~

Add a new graph Block

The height and width of the block are both 32. The margin and spacing are both 1px. These values depend on the resource of your graph block. The effect is as follows:

And draw a map.

Save the tmx file. Use gdx-tiled-preprocessor for processing.

After processing is complete, three more files will overwrite the original file with the same name:

Tilest1.png file:

Finally, you can start drawing...

map = TiledLoader.createMap(mapHandle); 
atlas = new TileAtlas(map, new FileHandle("map/"));
tileMapRenderer = new TileMapRenderer(map, atlas, 10, 10)

Finally, use tileMapRenderer. render (cam); In render to draw

In the TileMapRenderer (map, atlas, 10, 10) sentence, the next two 10 are the number of buffer blocks, which can be adjusted as appropriate. I wrote it at will.

I personally like the stage. In fact, tiledmap can be used in the stage.

I drew a label on the stage as an example.

In fact, the principle is very simple. Get Camera from Stage, use it for Render, and draw it in Stage.

Key code:

Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 
OrthographicCamera c = (OrthographicCamera) stage.getCamera();
((Label) stage.findActor("fpsLabel")).setText("FPS: "
+ Gdx.graphics.getFramesPerSecond());
stage.act(Gdx.graphics.getDeltaTime());
tileMapRenderer.render(c);
stage.draw();

Complete code:

package com.cnblogs.htynkn.game;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.tiled.TileAtlas;
import com.badlogic.gdx.graphics.g2d.tiled.TileMapRenderer;
import com.badlogic.gdx.graphics.g2d.tiled.TiledLoader;
import com.badlogic.gdx.graphics.g2d.tiled.TiledMap;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;

public class JavaGame implements ApplicationListener {

Stage stage;
float width;
float height;
private TiledMap map;
private TileAtlas atlas;
private TileMapRenderer tileMapRenderer;

Vector3 camDirection = new Vector3(1, 1, 0);
Vector2 maxCamPosition = new Vector2(0, 0);

Image image;

@Override
public void create() {
final String path = "map/";
final String mapname = "tilemap";
FileHandle mapHandle = Gdx.files.internal(path + mapname + ".tmx");
map = TiledLoader.createMap(mapHandle);
atlas = new TileAtlas(map, new FileHandle("map/"));
tileMapRenderer = new TileMapRenderer(map, atlas, 10, 10);
maxCamPosition.set(tileMapRenderer.getMapWidthUnits(), tileMapRenderer
.getMapHeightUnits());

width = Gdx.graphics.getWidth();
height = Gdx.graphics.getHeight();
stage = new Stage(width, height, true);
Label label = new Label("FPS:", new LabelStyle(new BitmapFont(Gdx.files
.internal("font/blue.fnt"),
Gdx.files.internal("font/blue.png"), false), Color.BLACK),
"fpsLabel");
stage.addActor(label);
Gdx.input.setInputProcessor(stage);
}

@Override
public void dispose() {
// TODO Auto-generated method stub

}

@Override
public void pause() {
// TODO Auto-generated method stub

}

@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
OrthographicCamera c = (OrthographicCamera) stage.getCamera();
((Label) stage.findActor("fpsLabel")).setText("FPS: "
+ Gdx.graphics.getFramesPerSecond());
stage.act(Gdx.graphics.getDeltaTime());
tileMapRenderer.render(c);
stage.draw();
}

@Override
public void resize(int width, int height) {
// TODO Auto-generated method stub

}

@Override
public void resume() {
// TODO Auto-generated method stub

}
}

The effect is not obvious. There is a Label in the lower left corner.

Although we have drawn a Map, but there are no roles, and no obstacles such as wall settings, these will be discussed in the following articles.

 

Conclusion:

1. The TMX file must be processed and used later. Use TiledMapPacker instead of TexturePacker. At first I got confused, and the result was that I understood the Code to reflect it...

2. When Stage and TiledMap are mixed, the stage must be drawn after.

3. The camera of the Stage can be moved, and the map moving effect (for example, the main character moving forward) will be generated when moving, but the Actors position of the Stage must be updated.

 

This blog post was written for a day and a half, mainly because I carelessly mistaken TiledMapPacker and TexturePacker. I carefully looked at TexturePacker and thought TexturePacker was also very useful. The algorithm is very interesting, and there is also a GUI made by enthusiastic netizens, so you don't have to work hard.

Algorithm Introduction: http://www.blackpawn.com/texts/lightmaps/default.html

TexturePacker: http://code.google.com/p/libgdx-texturepacker-gui/ for GUI

 

Note:

I do not have any problems when operating... if you encounter problems, Please carefully debugging, you can refer to: http://www.cnblogs.com/SkyD/archive/2012/04/19/2457237.html

 

Here there are Bo you broken wood guitar tilemap generation of the article: http://bluthmatter.blog.163.com/blog/static/18429405920124205458401/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.