android游戏开发框架libgdx的使用(十二)—TiledMap地图的使用

转自:http://www.apkbus.com/forum.php?mod=viewthread&tid=19770&extra=page%3D1%26filter%3Dauthor%26orderby%3Ddateline%26orderby%3Ddateline

 

虽说可以用Image什么的当个背景,但是要是做个RPG类的游戏就有点复杂了。为了追求效率一般可以使用libgdx的SpriteCache,但是如果习惯于TiledMap的话libgdx也是支持的。

相关的类是TiledMap,TileAtlas,TileMapRenderer,都在com.badlogic.gdx.graphics.g2d.tiled之中。

现在我们从头来看看TiledMap的使用。

1.制作TiledMap。

我使用的是Tile Map editor,下载地址:http://www.mapeditor.org/

先新建一个地图,大小50*30,每个块的大小是32*32。

然后我使用的图块资源是从网上下的,具体出处不知了,对于资源奉献者表示感谢~

添加一个新图块

块的高宽都是32.边距和间距都是1px,这些数值是取决你的图块资源本身的。效果如下:

然后发挥想象吧,画一幅地图。

5.png

2012-1-17 22:08 上传
下载附件(236.38 KB)

保存tmx文件。然后用gdx-tiled-preprocessor处理一下。

处理完成后会多三个文件,覆盖原来的同名文件:


6.jpg

2012-1-17 22:08 上传
下载附件(21.25 KB)

tilest1.png文件:

7.png

2012-1-17 22:08 上传
下载附件(111.46 KB)

终于可以开始绘制了…


  1. map = TiledLoader.createMap(mapHandle);
  2. atlas = new TileAtlas(map, new FileHandle("map/"));
  3. tileMapRenderer = new TileMapRenderer(map, atlas, 10, 10)
复制代码
最后在render用tileMapRenderer.render(cam);绘制而在TileMapRenderer(map, atlas, 10, 10)这句中后面两个10是缓冲的块数,可以酌情调整。我是随意写的。
8.jpg
2012-1-17 22:09 上传
下载附件(53.02 KB)


我个人比较喜欢舞台,其实tiledmap一样可以在舞台中使用。我在舞台绘制一个标签作为例子。其实原理很简单,从Stage获取Camera,然后给Render用,最后在Stage绘制。关键代码:
  1. Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  2. OrthographicCamera c = (OrthographicCamera) stage.getCamera();
  3. ((Label) stage.findActor("fpsLabel")).setText("FPS: "
  4. + Gdx.graphics.getFramesPerSecond());
  5. stage.act(Gdx.graphics.getDeltaTime());
  6. tileMapRenderer.render(c);
  7. stage.draw();
复制代码
完整代码:
  1. package com.cnblogs.htynkn.game;

  2. import com.badlogic.gdx.ApplicationListener;
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.files.FileHandle;
  5. import com.badlogic.gdx.graphics.Color;
  6. import com.badlogic.gdx.graphics.GL10;
  7. import com.badlogic.gdx.graphics.OrthographicCamera;
  8. import com.badlogic.gdx.graphics.g2d.BitmapFont;
  9. import com.badlogic.gdx.graphics.g2d.tiled.TileAtlas;
  10. import com.badlogic.gdx.graphics.g2d.tiled.TileMapRenderer;
  11. import com.badlogic.gdx.graphics.g2d.tiled.TiledLoader;
  12. import com.badlogic.gdx.graphics.g2d.tiled.TiledMap;
  13. import com.badlogic.gdx.math.Vector2;
  14. import com.badlogic.gdx.math.Vector3;
  15. import com.badlogic.gdx.scenes.scene2d.Stage;
  16. import com.badlogic.gdx.scenes.scene2d.ui.Image;
  17. import com.badlogic.gdx.scenes.scene2d.ui.Label;
  18. import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;

  19. public class JavaGame implements ApplicationListener {

  20. Stage stage;
  21. float width;
  22. float height;
  23. private TiledMap map;
  24. private TileAtlas atlas;
  25. private TileMapRenderer tileMapRenderer;

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

  28. Image image;

  29. @Override
  30. public void create() {
  31. final String path = "map/";
  32. final String mapname = "tilemap";
  33. FileHandle mapHandle = Gdx.files.internal(path + mapname + ".tmx");
  34. map = TiledLoader.createMap(mapHandle);
  35. atlas = new TileAtlas(map, new FileHandle("map/"));
  36. tileMapRenderer = new TileMapRenderer(map, atlas, 10, 10);
  37. maxCamPosition.set(tileMapRenderer.getMapWidthUnits(), tileMapRenderer
  38. .getMapHeightUnits());

  39. width = Gdx.graphics.getWidth();
  40. height = Gdx.graphics.getHeight();
  41. stage = new Stage(width, height, true);
  42. Label label = new Label("FPS:", new LabelStyle(new BitmapFont(Gdx.files
  43. .internal("font/blue.fnt"),
  44. Gdx.files.internal("font/blue.png"), false), Color.BLACK),
  45. "fpsLabel");
  46. stage.addActor(label);
  47. Gdx.input.setInputProcessor(stage);
  48. }

  49. @Override
  50. public void dispose() {
  51. // TODO Auto-generated method stub

  52. }

  53. @Override
  54. public void pause() {
  55. // TODO Auto-generated method stub

  56. }

  57. @Override
  58. public void render() {
  59. Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  60. OrthographicCamera c = (OrthographicCamera) stage.getCamera();
  61. ((Label) stage.findActor("fpsLabel")).setText("FPS: "
  62. + Gdx.graphics.getFramesPerSecond());
  63. stage.act(Gdx.graphics.getDeltaTime());
  64. tileMapRenderer.render(c);
  65. stage.draw();
  66. }

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

  70. }

  71. @Override
  72. public void resume() {
  73. // TODO Auto-generated method stub

  74. }
  75. }
复制代码
9.jpg
2012-1-17 22:09 上传
下载附件(60.96 KB)




效果不是很明显,左下角有个Label。虽说我们绘制出来了Map,但是还没有角色,而且还没有设置墙壁等障碍,接下来的几篇文章会说说这些。


写在最后:


1.做好的TMX文件一定要处理以后再用,用TiledMapPacker处理,不是TexturePacker。我起初弄混淆了,结果把这一块的代码读懂了才反应过来…
2.Stage和TiledMap混用时,一定是stage的绘制在后。


3.Stage的相机可以移动,移动时会产生地图移动效果(比如主角向前走),但是一定要更新Stage的Actors的位置。


这篇博文写了一天半,主要是我粗心把TiledMapPacker和TexturePacker搞错,仔细看了看TexturePacker,觉得TexturePacker也是很有用的东西。算法很有趣,而且还有热心网友做的一个GUI界面,用这个就不用自己辛苦拉图了


算法介绍: http://www.blackpawn.com/texts/lightmaps/default.html


GUI界面的TexturePacker: http://code.google.com/p/libgdx-texturepacker-gui/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值