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层,每按一次空格键就会切换一层,然后显示对应的粒子效果。

 

posted @   丛林小阁楼  阅读(659)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示