首页 > 其他 > 详细

(libgdx学习)AssetManager及进度条的绘制

时间:2014-04-02 09:56:42      阅读:594      评论:0      收藏:0      [点我收藏+]

进度条的绘制实际上就是根据已经加载资源的百分比来改变图片的长度


一、初始化及常见API

manager.load("animation.png",Texture.class);

manager.unload("animation.png");

manager.isLoaded("animation.png")

manager.get("animation.png",Texture.class)

manager.update();

manager.getProgress()


二、应用举例

这个是一个名为“奋斗的小土豆”的博客(可以直接在百度中搜一下)中所用到的源码

package com.example.groupactiontest;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Input.Peripheral;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.audio.Sound;
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.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class MyGame implements ApplicationListener {

	AssetManager manager;
	BitmapFont font;
	SpriteBatch batch;
	
	BitmapFont font2;
	TextureAtlas tex2;
	Texture tex1;
	
	Texture progressBar_BG;
	Texture progressBar_FT;
	TextureRegion region;
	float progress = 0;
	
	
	@Override
	public void create() {
		batch = new SpriteBatch();
		font = new BitmapFont(Gdx.files.internal("font.fnt"),false);
		manager = new AssetManager();
		progressBar_BG = new Texture("progressBar.png");
		progressBar_FT = new Texture("greenBar.png");
		region = new TextureRegion(progressBar_BG, 0, 0,512,64);
		
		load();
	}

	public void load(){
		tex1 = new Texture("animation.png");
		tex2 = new TextureAtlas(Gdx.files.internal("pack"));
		font2 = new BitmapFont(Gdx.files.internal("verdana39.fnt"),false);
		
		manager.load("animation.png",Texture.class);
		manager.load("pack1.png",Texture.class);
		manager.load("pack",TextureAtlas.class);
		manager.load("verdana39.png",Texture.class);
		manager.load("verdana39.fnt",BitmapFont.class);
		
	}
	
	public void unload(){
		tex1.dispose();
		tex2.dispose();
		font2.dispose();
		
		manager.unload("animation.png");
		manager.unload("pack1.png");
		manager.unload("pack");
		manager.unload("verdana39.png");
		manager.unload("verdana39.fnt");
	}
	
	@Override
	public void dispose() {
		unload();
	}

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

	}

	@Override
	public void render() {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
        manager.update();		
		
		batch.begin();
		
		if(manager.isLoaded("animation.png")){
			batch.draw(manager.get("animation.png",Texture.class), 50,50);
		}
		
		if(manager.isLoaded("verdana39.png")){
			batch.draw(manager.get("verdana39.png",Texture.class),250,150,150,150);
		}
		
		if(manager.isLoaded("pack")){
			batch.draw(manager.get("pack",TextureAtlas.class).findRegion("particle-star"),270,70);
		}
		
		if(manager.isLoaded("verdana39.fnt")){
			manager.get("verdana39.fnt",BitmapFont.class).draw(batch, "This is a test", 100, 200);
		}
		
		if(manager.isLoaded("back.ogg",Sound.class)){
			manager.get("back.ogg",Sound.class).play();
		}
		
		progress = manager.getProgress()*512;
		font.draw(batch, "Progress :" + manager.getProgress()*100 +"%", 160, 15);
		batch.draw(progressBar_FT, 40, 0, progress, 54);
		batch.draw(region, 0, 20);
		
		batch.end();
	}

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

	}

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

	}

}


以下是我理解的源码

package com.example.groupactiontest;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Input.Peripheral;
import com.badlogic.gdx.assets.AssetManager;
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.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class MyGame implements ApplicationListener {

	AssetManager manager;
	SpriteBatch batch;
	BitmapFont font;
	
	Texture background_FT;
	Texture background_BG;
	TextureRegion region;
	
	float progress;
	
	@Override
	public void create() {
		manager = new AssetManager();
		batch = new SpriteBatch();
		font = new BitmapFont(Gdx.files.internal("font.fnt"), false);
		
		background_BG = new Texture(Gdx.files.internal("progressBar.png"));
		background_FT = new Texture(Gdx.files.internal("greenBar.png"));
		region = new TextureRegion(background_BG , 0, 0,512,64);
		
		load();
	}
	
	public void load(){
		manager.load("animation.png",Texture.class);
		manager.load("pack",TextureAtlas.class);
		manager.load("pack1.png",Texture.class);
		manager.load("verdana39.fnt",BitmapFont.class);
		manager.load("verdana39.png",Texture.class);
		
	}

	public void unload(){
		font.dispose();
		batch.dispose();
		
		manager.unload("animation.png");
		manager.unload("pack");
		manager.unload("pack1.png");
		manager.unload("verdana39.png");
		manager.unload("verdana39.fnt");
		
		
	}
	
	
	@Override
	public void dispose() {
		unload();

	}

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

	}

	@Override
	public void render() {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		manager.update();
		
		batch.begin();
		
		if(manager.isLoaded("animation.png",Texture.class)){
			batch.draw(manager.get("animation.png", Texture.class), 0,100);
		}
		
		if(manager.isLoaded("verdana39.png",Texture.class)){
			batch.draw(manager.get("verdana39.png",Texture.class),100,100);
		}
		
		if(manager.isLoaded("verdana39.fnt",BitmapFont.class)){
			manager.get("verdana39.fnt",BitmapFont.class).draw(batch, "hello AssetManager", 0, 300);
		}
		
		if(manager.isLoaded("pack",TextureAtlas.class)){
			batch.draw(manager.get("pack", TextureAtlas.class).findRegion("particle-star"), 200,0);
		}
		
		if(manager.isLoaded("pack1.png",Texture.class)){
			batch.draw(manager.get("pack1.png",Texture.class), 300,300);
		}
		
		progress = manager.getProgress()*512;
		
		font.draw(batch, "Progress: " + manager.getProgress()*100 +"%" ,160, 15);
		batch.draw(background_FT, 40, 0, progress, 54);
		batch.draw(region, 0, 20);
		
		batch.end();
		
	}

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

	}

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

	}

}



四、源码下载

http://download.csdn.net/detail/caihongshijie6/7129269

(libgdx学习)AssetManager及进度条的绘制,布布扣,bubuko.com

(libgdx学习)AssetManager及进度条的绘制

原文:http://blog.csdn.net/hjd_love_zzt/article/details/22724385

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!