libgdx学习记录10——Particle粒子
粒子对制作画面特效很有用,可以使用Particle Editor进行自行编辑粒子效果,跟图片一起生成.p粒子文件,然后导入到程序中使用。
本文所用的粒子效果是基于其自带的demo的。
实例:
1 package com.fxb.newtest; 2 3 import com.badlogic.gdx.ApplicationAdapter; 4 import com.badlogic.gdx.Gdx; 5 import com.badlogic.gdx.Input; 6 import com.badlogic.gdx.InputAdapter; 7 import com.badlogic.gdx.InputProcessor; 8 import com.badlogic.gdx.graphics.GL10; 9 import com.badlogic.gdx.graphics.g2d.ParticleEffect; 10 import com.badlogic.gdx.graphics.g2d.ParticleEmitter; 11 import com.badlogic.gdx.graphics.g2d.SpriteBatch; 12 import com.badlogic.gdx.utils.Array; 13 14 public class Lib008_Particle extends ApplicationAdapter{ 15 16 ParticleEffect effect; 17 InputProcessor processor; 18 19 float positionX; 20 float positionY; 21 22 SpriteBatch batch; 23 24 Array<ParticleEmitter> sEmitter; 25 int index = 0; 26 int maxSize; 27 28 @Override 29 public void create() { 30 // TODO Auto-generated method stub 31 batch = new SpriteBatch(); 32 effect = new ParticleEffect(); 33 effect.load( Gdx.files.internal("particle/test.p"), Gdx.files.internal("particle/") ); 34 maxSize = effect.getEmitters().size; 35 36 positionX = Gdx.graphics.getWidth()/2; 37 positionY = Gdx.graphics.getHeight()/2; 38 39 //processor = new InputProcessor(){ 40 processor = new InputAdapter(){ 41 @Override 42 public boolean keyDown(int keycode) { 43 // TODO Auto-generated method stub 44 if( keycode == Input.Keys.SPACE ){ 45 index = (++index)%maxSize; 46 effect.getEmitters().clear(); 47 effect.getEmitters().add( sEmitter.get(index) ); 48 } 49 return false; 50 } 51 @Override 52 public boolean touchDragged(int screenX, int screenY, int pointer) { 53 // TODO Auto-generated method stub 54 positionX = screenX; 55 positionY = Gdx.graphics.getHeight()-screenY; 56 return false; 57 } 58 }; 59 60 Gdx.input.setInputProcessor( processor ); 61 62 sEmitter = new Array<ParticleEmitter>(); 63 for( ParticleEmitter emitter : effect.getEmitters() ){ 64 sEmitter.add( emitter ); 65 } 66 effect.getEmitters().clear(); 67 effect.getEmitters().add( sEmitter.get(index) ); 68 } 69 70 71 @Override 72 public void render() { 73 // TODO Auto-generated method stub 74 Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT ); 75 effect.setPosition( positionX, positionY ); 76 77 float delta = Gdx.graphics.getDeltaTime(); 78 batch.begin(); 79 effect.draw( batch, delta ); 80 batch.end(); 81 82 } 83 84 @Override 85 public void dispose() { 86 // TODO Auto-generated method stub 87 batch.dispose(); 88 effect.dispose(); 89 } 90 91 }
运行效果:
这个粒子一共有4层,每按一次空格键就会切换一层,然后显示对应的粒子效果。