LibGdx--粒子效果

package com.joye3g.particletest;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.ParticleEmitter;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;

public class ParticleTest extends InputAdapter implements ApplicationListener{

	private SpriteBatch batch;
	private ParticleEffect effect;
	private Array<ParticleEmitter> emitters;
	private Vector2 position;
	private int emitterIndex = 1;
	@Override
	public void create() {
		//实例化粒子效果
		effect = new ParticleEffect();
		//加载粒子效果
		effect.load(Gdx.files.internal("data/testcat.p"), Gdx.files.internal("data/"));
		//取得所有的发射器
		emitters = new Array<ParticleEmitter>(effect.getEmitters());
		//清除所有的发射器
		effect.getEmitters().clear();
		//添加第一个发射器
		effect.getEmitters().add(emitters.get(0));
		batch = new SpriteBatch();
		Gdx.input.setInputProcessor(this);
		position = new Vector2();
	}

	@Override
	public void resize(int width, int height) {
	}

	@Override
	public void render() {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		batch.begin();
		effect.setPosition(position.x, position.y);
		effect.draw(batch, Gdx.graphics.getDeltaTime());
		batch.end();
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}

	@Override
	public void dispose() {
	}
	
	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
		//取得下一个发射器的下标
		emitterIndex = (emitterIndex + 1) % emitters.size;
		//取得下一个发射器
		ParticleEmitter emitter = emitters.get(emitterIndex);
		//清除所有的发射器
		effect.getEmitters().clear();
		//添加发射器
		effect.getEmitters().add(emitter);
		//设置位置
		position.x = screenX;
		position.y = Gdx.graphics.getHeight() - screenY;
		return super.touchDown(screenX, screenY, pointer, button);
	}
}


资源下载:http://download.csdn.net/detail/zlqqhs/5383645

解压缩后放到Android工程下的assets下的data目录下即可

效果图:

LibGdx--粒子效果_第1张图片

你可能感兴趣的:(libgdx,Particle,粒子,粒子效果,发射器)