转自:http://blog.csdn.net/xn4545945/article/details/9164867#comments
留心的朋友会发现,安装完微信4.5后,会有一个跳动的火焰、并播放《一无所有》的欢迎页。本篇文章模拟的是火焰跳动的效果。
一、LibGdx游戏引擎介绍
LibGdx是一款基于OpenGL ES技术开发的Android游戏引擎,支持Android平台下的2D游戏开发,物理引擎采用Box2D实现
LibGdx是一个跨平台的2D/3D的游戏开发框架,它由Java/C/C++语言编写而成。它基于Apache License, Version 2.0协议,对商业使用和非商业使用均免费。代码托管于Github中。最新版本为0.9.8。
官方网站:http://code.google.com/p/libgdx/
LibGdx单就性能角度来说,堪称是一款非常强大的 Android游戏引擎。
二、粒子特效
为模拟现实中的水、火、雾、气等效果由各种三维软件开发的制作模块,原理是将无数的单个粒子组合使其呈现出固定形态,借由控制器,脚本来控制其整体或单个的运动,模拟出现真实的效果。
三、LibGdx粒子特效编辑器
下载地址:http://code.google.com/p/libgdx/wiki/ParticleEditor
用来编辑粒子效果的编辑器。(参数可以任意尝试设置)
打开后界面如下, 可直接保存效果为my.p,为后续使用。
四、具体实现
功能:实现一个画面,当手触摸时,火焰随之移动。
核心代码:(my.p放在asset/data目录下,并在该目录下放一张particle.png(为单个粒子的图片文件,文件名唯一))
[java]view plaincopyprint?
- package com.example.libgdx_02_Particle;
- import com.badlogic.gdx.ApplicationListener;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.InputProcessor;
- 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.utils.Array;
- public class Game implements ApplicationListener
- {
-
- SpriteBatch batch;
- ParticleEffect effect;
- int index;
- Array<ParticleEmitter> emitters;
- int count;
-
- //一旦ApplicationListener开始运行,render函数就会不停执行
- @Override
- public void render()//着色, 渲染.
- {
- // TODO Auto-generated method stub
- Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);//清屏
- Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
-
- batch.getProjectionMatrix().setToOrtho2D(0, 0, Gdx.graphics.getWidth(),
- Gdx.graphics.getHeight());
- Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
- batch.begin();
- effect.draw(batch, Gdx.graphics.getDeltaTime());
- batch.end();
-
- }
-
- @Override
- public void create()
- {
- //初始化
- batch = new SpriteBatch();
- effect = new ParticleEffect();
- //加载文件
- effect.load(Gdx.files.internal("data/my.p"),
- Gdx.files.internal("data/"));
- //设置特效位置
- effect.setPosition(Gdx.graphics.getWidth() / 2,
- Gdx.graphics.getHeight() / 2);
-
- emitters = new Array<ParticleEmitter>(effect.getEmitters());
- effect.getEmitters().clear();
- effect.getEmitters().add(emitters.get(index));
-
- //点击屏幕移动, 设置输入
- InputProcessor inputProcessor = new InputProcessor()
- {
-
- @Override
- public boolean touchUp(int arg0, int arg1, int arg2, int arg3)
- {
- return false;
- }
-
- @Override
- public boolean touchDragged(int arg0, int arg1, int arg2)
- {
- effect.setPosition(arg0, Gdx.graphics.getHeight() - arg1);
- return false;
- }
-
- @Override
- public boolean touchDown(int arg0, int arg1, int arg2, int arg3)
- {
- ParticleEmitter emitter = emitters.get(index);
- count += 100;
- if (count > emitter.getMaxParticleCount())
- emitter.setMaxParticleCount(count * 2);
- count = Math.max(0, count);
- emitter.getEmission().setHigh(
- count / emitter.getLife().getHighMax() * 1000);
- effect.getEmitters().clear();
- effect.getEmitters().add(emitter);
- return false;
- }
-
- @Override
- public boolean scrolled(int arg0)
- {
- // TODO Auto-generated method stub
- return false;
- }
-
- @Override
- public boolean keyUp(int arg0)
- {
- // TODO Auto-generated method stub
- return false;
- }
-
- @Override
- public boolean keyTyped(char arg0)
- {
- // TODO Auto-generated method stub
- return false;
- }
-
- @Override
- public boolean keyDown(int arg0)
- {
- // TODO Auto-generated method stub
- return false;
- }
-
- @Override
- public boolean mouseMoved(int arg0, int arg1)
- {
- // TODO Auto-generated method stub
- return false;
- }
- };
- Gdx.input.setInputProcessor(inputProcessor);
- }
-
- @Override
- public void dispose()
- {
- // 释放资源
- batch.dispose();
-
- }
-
- @Override
- public void pause()
- {
- // TODO Auto-generated method stub
-
- }
-
- @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.libgdx_02_Particle;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
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.utils.Array;
public class Game implements ApplicationListener
{
SpriteBatch batch;
ParticleEffect effect;
int index;
Array<ParticleEmitter> emitters;
int count;
//一旦ApplicationListener开始运行,render函数就会不停执行
@Override
public void render()//着色, 渲染.
{
// TODO Auto-generated method stub
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);//清屏
Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
batch.getProjectionMatrix().setToOrtho2D(0, 0, Gdx.graphics.getWidth(),
Gdx.graphics.getHeight());
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
effect.draw(batch, Gdx.graphics.getDeltaTime());
batch.end();
}
@Override
public void create()
{
//初始化
batch = new SpriteBatch();
effect = new ParticleEffect();
//加载文件
effect.load(Gdx.files.internal("data/my.p"),
Gdx.files.internal("data/"));
//设置特效位置
effect.setPosition(Gdx.graphics.getWidth() / 2,
Gdx.graphics.getHeight() / 2);
emitters = new Array<ParticleEmitter>(effect.getEmitters());
effect.getEmitters().clear();
effect.getEmitters().add(emitters.get(index));
//点击屏幕移动, 设置输入
InputProcessor inputProcessor = new InputProcessor()
{
@Override
public boolean touchUp(int arg0, int arg1, int arg2, int arg3)
{
return false;
}
@Override
public boolean touchDragged(int arg0, int arg1, int arg2)
{
effect.setPosition(arg0, Gdx.graphics.getHeight() - arg1);
return false;
}
@Override
public boolean touchDown(int arg0, int arg1, int arg2, int arg3)
{
ParticleEmitter emitter = emitters.get(index);
count += 100;
if (count > emitter.getMaxParticleCount())
emitter.setMaxParticleCount(count * 2);
count = Math.max(0, count);
emitter.getEmission().setHigh(
count / emitter.getLife().getHighMax() * 1000);
effect.getEmitters().clear();
effect.getEmitters().add(emitter);
return false;
}
@Override
public boolean scrolled(int arg0)
{
// TODO Auto-generated method stub
return false;
}
@Override
public boolean keyUp(int arg0)
{
// TODO Auto-generated method stub
return false;
}
@Override
public boolean keyTyped(char arg0)
{
// TODO Auto-generated method stub
return false;
}
@Override
public boolean keyDown(int arg0)
{
// TODO Auto-generated method stub
return false;
}
@Override
public boolean mouseMoved(int arg0, int arg1)
{
// TODO Auto-generated method stub
return false;
}
};
Gdx.input.setInputProcessor(inputProcessor);
}
@Override
public void dispose()
{
// 释放资源
batch.dispose();
}
@Override
public void pause()
{
// TODO Auto-generated method stub
}
@Override
public void resize(int arg0, int arg1)
{
// TODO Auto-generated method stub
}
@Override
public void resume()
{
// TODO Auto-generated method stub
}
}
;
运行效果:
有没有比微信还炫呢??
更多资料: http://www.apkbus.com/android-57456-1-1.html
源码下载 :http://download.csdn.net/detail/xn4545945/5643035
原创文章,转载请注明出处:http://blog.csdn.net/xn4545945