android游戏开发框架libgdx的使用(十九)—使用自定义配置改进AVG游戏开发
来源:互联网 发布:大数据的权威期刊 编辑:程序博客网 时间:2024/05/18 13:46
转自:http://www.apkbus.com/forum.php?mod=viewthread&tid=44514&extra=page%3D1%26filter%3Dauthor%26orderby%3Ddateline%26orderby%3Ddateline
先说明一下上一篇文章我使用了多张hiero图的字体绘制,因为我对源码进行了一些修改,本来想这次发出来的,但是我仔细调试了一下,发现对于多图的支持还是有问题,有些字会出现偏移。
这个只有继续尝试了…大家可以考虑使用ttf字库。
然后继续说上一篇,虽然实现了一个简单的效果,但是目前有很多不足。我把AVG游戏需要的资源全部提取出来,放在一个个文件夹中,然后通过配置文件加载这些数据。
libgdx的工具库com.badlogic.gdx.utils就是libgdx的工具库,支持两种格式xml和json。
我最先倾向于使用json格式,但是反复想了想,虽然json的大小可能要小点,但是没有xml直观好读。
所以还是选择使用xml格式,读取xml文件使用XmlReader。
- XmlReader reader=new XmlReader();
- try {
- Element config=reader.parse(Gdx.files.internal("data/config.xml"));
- config.get("name"); //获取属性值,如果没有属性值则返回同名child的值
- config.getAttribute("name"); //获取属性值
- } catch (IOException e) {
- e.printStackTrace();
- }
配置文件需要配置的内容其实就是上一篇我们硬编码进去的东西,有背景、边框、对话等。
我设计的比较随意,不一定是最好的,大家可以参考参考。
- <?xml version="1.0" encoding="UTF-8"?>
- <scene>
- <packfile>pack</packfile>
- <background>bg1</background>
- <border>
- <border-name>border</border-name>
- <border-left>26</border-left>
- <border-right>26</border-right>
- <border-top>31</border-top>
- <border-bottom>31</border-bottom>
- </border>
- <dialogues>
- <dialogue>
- <name>人物1</name>
- <data>这是对话1</data>
- </dialogue>
- <dialogue>
- <name>人物1</name>
- <data>这是对话2</data>
- </dialogue>
- </dialogues>
- </scene>
如果对话比较长还可以继续添加节点。border-*那4个值就是NinePatch的左边距、右边距、上边距、下边距。
这里还有一个地方需要注意,如果我们需要切换场景怎么办?切换的场景可能是一个AVG场景,也可能是个一般的Screen。我本来想做个简单的IOC容器的,但后来想想也没有那么复杂。
在配置文件中加上一句
如果是一般Screen就是
- com.cnblogs.htynkn.screen.GameOver
- data/scene2/config.xml
其实整个原理和上一篇原理是一样的,唯一多的一个就是解析配置文件和场景跳转。
- XmlReader xmlReader = new XmlReader();
- try {
- Element config = xmlReader.parse(Gdx.files
- .internal(this.configFileName)); //加载配置文件
- atlas = new TextureAtlas(Gdx.files.internal(configFileName)
- .parent()
- + "/" + config.get("packfile")); //获取pack配置文件
- background = atlas.findRegion(config.get("background")); //创建背景图
- Element borderConfig = config.getChildByName("border"); //获取border配置
- border = new NinePatch(atlas.findRegion(borderConfig.getChild(0)
- .getText()), Integer.parseInt(borderConfig.getChild(1)
- .getText()), Integer.parseInt(borderConfig.getChild(2)
- .getText()), Integer.parseInt(borderConfig.getChild(3)
- .getText()), Integer.parseInt(borderConfig.getChild(4)
- .getText())); //实例化border
- dialogues = new ArrayList();
- Element dialoguesConfig = config.getChildByName("dialogues"); //开始处理对话
- for (int i = 0; i < dialoguesConfig.getChildCount(); i++) {
- dialogues.add(new String[] {
- dialoguesConfig.getChild(i).get("name"),
- dialoguesConfig.getChild(i).get("data") });
- }
- Element screenConfig = config.getChildByName("nextscreen"); //处理场景
- if (screenConfig.getAttribute("type").equals("avg")) {
- nextScreen = new AVGScreen(this.game, screenConfig.getText());
- } else {
- try {
- nextScreen = (Screen) Class.forName(screenConfig.getText())
- .newInstance();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- borderImage.setClickListener(new ClickListener() {
- @Override
- public void click(Actor actor, float x, float y) {
- if (currentSize < dialogues.size() - 1) {
- currentSize++;
- } else {
- if (nextScreen != null) {
- game.setScreen(nextScreen);
- }
- }
- }
- });
- package com.cnblogs.htynkn.ui;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import com.badlogic.gdx.Game;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Screen;
- import com.badlogic.gdx.graphics.Color;
- import com.badlogic.gdx.graphics.GL10;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.BitmapFont;
- import com.badlogic.gdx.graphics.g2d.NinePatch;
- import com.badlogic.gdx.graphics.g2d.TextureAtlas;
- import com.badlogic.gdx.graphics.g2d.TextureRegion;
- import com.badlogic.gdx.scenes.scene2d.Actor;
- import com.badlogic.gdx.scenes.scene2d.Stage;
- import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
- 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.utils.Array;
- import com.badlogic.gdx.utils.XmlReader;
- import com.badlogic.gdx.utils.XmlReader.Element;
- public class AVGScreen implements Screen {
- private String configFileName;
- private Stage stage; // 舞台
- private List dialogues; // 对话
- private BitmapFont bitmapFont; // 文字
- private NinePatch border; // 边框
- private int currentSize; // 当前对话序号
- private TextureAtlas atlas;
- private TextureRegion background;
- private Screen nextScreen;
- private Game game;
- public AVGScreen(Game game, String configFileName) {
- this.game = game;
- bitmapFont = new BitmapFont(Gdx.files.internal("font/chinese.fnt"),
- false);
- this.configFileName = configFileName;
- XmlReader xmlReader = new XmlReader();
- try {
- Element config = xmlReader.parse(Gdx.files
- .internal(this.configFileName));
- atlas = new TextureAtlas(Gdx.files.internal(configFileName)
- .parent()
- + "/" + config.get("packfile"));
- background = atlas.findRegion(config.get("background"));
- Element borderConfig = config.getChildByName("border");
- border = new NinePatch(atlas.findRegion(borderConfig.getChild(0)
- .getText()), Integer.parseInt(borderConfig.getChild(1)
- .getText()), Integer.parseInt(borderConfig.getChild(2)
- .getText()), Integer.parseInt(borderConfig.getChild(3)
- .getText()), Integer.parseInt(borderConfig.getChild(4)
- .getText()));
- dialogues = new ArrayList();
- Element dialoguesConfig = config.getChildByName("dialogues");
- for (int i = 0; i < dialoguesConfig.getChildCount(); i++) {
- dialogues.add(new String[] {
- dialoguesConfig.getChild(i).get("name"),
- dialoguesConfig.getChild(i).get("data") });
- }
- Element screenConfig = config.getChildByName("nextscreen");
- if (screenConfig.getAttribute("type").equals("avg")) {
- nextScreen = new AVGScreen(this.game, screenConfig.getText());
- } else {
- try {
- nextScreen = (Screen) Class.forName(screenConfig.getText())
- .newInstance();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void dispose() {
- bitmapFont.dispose();
- stage.dispose();
- }
- @Override
- public void hide() {
- // TODO Auto-generated method stub
- }
- @Override
- public void pause() {
- // TODO Auto-generated method stub
- }
- @Override
- public void render(float delta) {
- Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
- ((Label) stage.findActor("label"))
- .setText(dialogues.get(currentSize)[0] + " : "
- + dialogues.get(currentSize)[1]);
- stage.act(Gdx.graphics.getDeltaTime());
- stage.draw();
- }
- @Override
- public void resize(int width, int height) {
- // TODO Auto-generated method stub
- }
- @Override
- public void resume() {
- // TODO Auto-generated method stub
- }
- @Override
- public void show() {
- currentSize = 0;
- stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(),
- false);
- Image backgroundImage = new Image(background);
- backgroundImage.x = backgroundImage.y = 0;
- backgroundImage.setFillParent(true);
- Image borderImage = new Image(border);
- borderImage.x = borderImage.y = 0;
- borderImage.width = Gdx.graphics.getWidth();
- borderImage.height = Gdx.graphics.getHeight() / 4;
- borderImage.setClickListener(new ClickListener() {
- @Override
- public void click(Actor actor, float x, float y) {
- if (currentSize < dialogues.size() - 1) {
- currentSize++;
- } else {
- if (nextScreen != null) {
- game.setScreen(nextScreen);
- }
- }
- }
- });
- LabelStyle labelStyle = new LabelStyle(bitmapFont, Color.WHITE);
- Label label = new Label("", labelStyle, "label");
- label.x = border.getLeftWidth() + 10;
- label.y = borderImage.height - border.getTopHeight() - 10;
- stage.addActor(backgroundImage);
- stage.addActor(borderImage);
- stage.addActor(label);
- Gdx.input.setInputProcessor(stage);
- }
- }
最后贴上效果图,这是我正在做的一个东西:
- android游戏开发框架libgdx的使用(十九)—使用自定义配置改进AVG游戏开发
- android游戏开发框架libgdx的使用(十八)—简单的AVG游戏效果实现
- Android游戏开发框架libgdx的使用
- Android游戏开发框架libgdx的使用
- android游戏开发框架libgdx的使用(九)—在libgdx中使用Box2d
- android游戏开发框架libgdx的使用(九)—在libgdx中使用Box2d
- android游戏开发框架libgdx的使用(十六)—使用TexturePacker工具加快开发速度
- android游戏开发框架libgdx的使用(十六)—使用TexturePacker工具加快开发速度
- android游戏开发框架libgdx的使用(十一)—Skin和UI配置文件的使用
- android游戏开发框架libgdx的使用(十二)—TiledMap地图的使用
- android游戏开发框架libgdx的使用(十八)—资源加载管理AssetManager的使用
- android游戏开发框架libgdx的使用(十一)—Skin和UI配置文件的使用
- android游戏开发框架libgdx的使用(十二)—TiledMap地图的使用
- Android游戏开发框架Libgdx的使用(一)--Libgdx基础入门
- android游戏开发框架libgdx的使用-环境搭建
- android游戏开发框架libgdx的使用(七)--开发框架总观
- android游戏开发框架libgdx的使用(七)--开发框架总观
- android游戏开发框架libgdx的使用(十七)—TiledMap中角色的行动路径
- android游戏开发框架libgdx的使用(十八)—简单的AVG游戏效果实现
- ubuntu上安装bochs
- 求职经验贴(收集中。。。,非本人的经验,内含转载地址)
- 绝大多数新技术的学习,都是浪费生命(转)
- 实现调用任意函数的代码
- android游戏开发框架libgdx的使用(十九)—使用自定义配置改进AVG游戏开发
- T4模板调试方法
- error LNK2001
- objectARX 常用功能实现集合
- [windows_internals]Stack Frame
- 桥接两无线路由—路由共享无线上网
- CS2 Downloads
- android游戏开发框架libgdx的使用(二十)—资源预加载与AssetManager的使用
- POJ-1159(Palindrome)