package com.badlogic.gdx.graphics.g2d.tiled;
文件TileAtlas:
当 libgdx使用tiled Map时,由于原方法获取图块是通过配置的图片packfile来获取的,用于我找不到生成图片packfile的工具,所以打算修改这个方法,直接不需要packfile就可以操作(一个方法优化可以少生成一个packfile文件),以下是原始方法:
public TileAtlas (TiledMap map, FileHandle inputDir) {
// TODO: Create a constructor that doesn't take a tmx map,
for (TileSet set : map.tileSets) {
FileHandle packfile = getRelativeFileHandle(inputDir, removeExtension(set.imageName) + " packfile");
TextureAtlas textureAtlas = new TextureAtlas(packfile, packfile.parent(), false);
List<AtlasRegion> atlasRegions = textureAtlas.findRegions(removeExtension(removePath(set.imageName)));
for (AtlasRegion reg : atlasRegions) {
regionsMap.put(reg.index + set.firstgid, reg);
if (!textures.contains(reg.getTexture())) {
textures.add(reg.getTexture());
}
}
}
}
修改为:通过算数来算出值,而不是通过packfile文件读取,package file读取可能会产生很多问题。
public TileAtlas (TiledMap map, FileHandle inputDir) {
for (TileSet set : map.tileSets) {
if(set.imageName==null){
continue;
}
FileHandle imageFile = getRelativeFileHandle(inputDir, (set.imageName) );
List<AtlasRegion> atlasRegions=new ArrayList<AtlasRegion>();
Texture texture = new Texture(imageFile);
for(int j=0;j<texture.getHeight()/set.tileHeight;j++){
int wn=texture.getWidth()/set.tileWidth;
for(int i=0;i<wn;i++){
AtlasRegion atlasRegion= new AtlasRegion(texture,i*set.tileWidth,j*set.tileHeight,set.tileWidth,set.tileHeight);
atlasRegion.index=j*wn+i;
atlasRegions.add(atlasRegion);
}
}
for (AtlasRegion reg : atlasRegions) {
regionsMap.put(reg.index + set.firstgid, reg);
if (!textures.contains(reg.getTexture())) {
textures.add(reg.getTexture());
}
}
}
}
我这里有一个demo,截图如下
代码如下:
package com.zx;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
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.Texture;
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.TileSet;
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;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
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() {
FileHandle mapHandle = Gdx.files.internal("map/ff.tmx");
System.out.println("MapHandle is -->"+mapHandle);
map = TiledLoader.createMap(mapHandle);
System.out.println("Map is -->"+map);
FileHandle packages=Gdx.files.internal("map");
System.out.println("Package fileHandle-->"+packages);
atlas = new CustomerTiledAlisa(map, packages);
tileMapRenderer = new TileMapRenderer(map, atlas, 10, 10);
System.out.println("TileMapRenderer--->"+tileMapRenderer);
maxCamPosition.set(tileMapRenderer.getMapWidthUnits(), tileMapRenderer .getMapHeightUnits());
width = Gdx.graphics.getWidth();
height = Gdx.graphics.getHeight();
stage = new Stage(width, height, true);
System.out.printf("Stage->(%f,%f)",width,height);
Label label = new Label("FPS:", new LabelStyle(new BitmapFont(Gdx.files
.internal("zh-cn.fnt"),
Gdx.files.internal("zh-cn.png"), false), Color.BLACK),
"fpsLabel");
System.out.println("Label--->"+label.toString());
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() {
System.out.println("start to render map");
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
OrthographicCamera c = (OrthographicCamera) stage.getCamera();
System.out.println("获取相机->"+c.toString());
((Label) stage.findActor("fpsLabel")).setText("FPS: "
+ Gdx.graphics.getFramesPerSecond());
stage.act(Gdx.graphics.getDeltaTime());
System.out.println("延迟时间-->"+Gdx.graphics.getDeltaTime());
System.out.println("Render(height,width)"+tileMapRenderer.getMapHeightUnits()+","+tileMapRenderer.getMapWidthUnits()+",Map->"+tileMapRenderer.getMap().tileSets);
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
}
private static class CustomerTiledAlisa extends TileAtlas{
private static FileHandle getRelativeFileHandle (FileHandle path, String relativePath) {
if (relativePath.trim().length() == 0) {
return path;
}
FileHandle child = path;
StringTokenizer tokenizer = new StringTokenizer(relativePath, "\\/");
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
if (token.equals("..")) {
child = child.parent();
} else {
child = child.child(token);
}
}
return child;
}
public CustomerTiledAlisa (TiledMap map, FileHandle inputDir) {
for (TileSet set : map.tileSets) {
if(set.imageName==null){
continue;
}
FileHandle imageFile = getRelativeFileHandle(inputDir, (set.imageName) );
List<AtlasRegion> atlasRegions=new ArrayList<AtlasRegion>();
Texture texture = new Texture(imageFile);
for(int j=0;j<texture.getHeight()/set.tileHeight;j++){
int wn=texture.getWidth()/set.tileWidth;
for(int i=0;i<wn;i++){
AtlasRegion atlasRegion= new AtlasRegion(texture,i*set.tileWidth,j*set.tileHeight,set.tileWidth,set.tileHeight);
atlasRegion.index=j*wn+i;
atlasRegions.add(atlasRegion);
}
}
for (AtlasRegion reg : atlasRegions) {
regionsMap.put(reg.index + set.firstgid, reg);
if (!textures.contains(reg.getTexture())) {
textures.add(reg.getTexture());
}
}
System.out.println("textuare is ->"+textures);
}
}
}
}
相关推荐
libgdx-nightly-20140711.zip libgdx 1.2.0
libgdx---MySuperJumper
libgdx-nightly-20110809.zip
libgdx-cookbook-cookbook的书中示例代码
libgdx-演示-metagun Libgdx 演示 --metagun
libGDX游戏引擎官网地址 What's libGDX? libGDX is a cross-platform Java game development framework based on OpenGL (ES) that works on Windows, Linux, Mac OS X, Android, your WebGL enabled ...《libGDX系列
类似俄罗斯方块小游戏,拖动组合方块,消除得分 类似俄罗斯方块小游戏,拖动组合方块,消除得分 类似俄罗斯方块小游戏,拖动组合方块,消除得分
2048 是一款非常热门的数字益智类游戏,操作规则很简单,在屏幕中有四行四列共 16 个卡片,初始时随机在其中两个卡片生成两个数字 2 或 4,然后手指可在卡片组区域 上下左右滑动 将所有卡片移动到 上侧 / 下侧 / ...
如何使用libgdx编写一个简单的游戏(一)— 雏形。...教程地址:http://www.huangyunkun.com/2013/02/14/libgdx-game-1/?c819a6de2f0b491b84f298fd67f01dda=88dbcf12c4e84a29ba9cabf8655c914b 此码使用libgdx0.99包
libGDX-十六进制瓷砖测试 libGDX的六边形拼贴功能的个人测试。 最初的起点是“ 和我自己的“。
libgdx-设计-编辑器
libgdx-crypt-texture 一个用于加密和解密文件的简单库(包括libGdx Texture) 用法 更新2018 为了方便起见,请使用。 忽略以下步骤。 步骤1.使用其他工具创建纹理文件(例如 ) 步骤2.通过libgdx-crypt-texture可...
LibGDX-SocketIO-带节点 安装并运行节点。 将directoy更改为服务器文件夹。 确保已安装Node JS。 终端,“ npm install” 终端,节点server.js 成功。 应用程式设定 在“ sockettest / core / src / ...
https://github.com/libgdx/libgdx-demo-cuboc 基于libgdx-demo-cubu开发的demo。
LibGDX瓷砖渲染器 此实验旨在突破LibGDX的图块渲染功能的局限性。 目标是尽可能多地利用GPU来渲染非常大的平铺世界。 我们通过在GPU本身上存储切片顶点/几何来实现此目的,从而减少了昂贵的访问时间。 结果使我们...
Libgdx开发丛书之 Libgdx cross-platform game development cookbook
o2d-libgdx-setup-plugin 插件到无限制的libGDX设置应用程序
libgdx-pixel-runner Libgdx port van de moai-pixel-runner
Libgdx专题系列 斜45°地图 代码工程下载
Over 75 practical recipes to help you master cross-platform 2D game development using the powerful Libgdx framework