我想在libgdx中创建一个粒子系统。我知道libgdx中有粒子类,但我想自己创建它。粒子系统是关于雪。我写了一个班级,只有一个雪从顶部到地面,从它向左和向右移动(这么少)。 这是我的班级:
public class Particle implements ApplicationListener{
SpriteBatch batch;
Texture texture;
Random rand = new Random();
float randX = rand.nextFloat() * 480;
float randY = 322;
Pixmap circle;
boolean gravity = true;
Vector2 Vgravity;
Vector2 position;
float snowMovement;
int j = 1;
@Override
public void create() {
if (randX < 10) {
randX = 10;
}else if (randX > 460) {
randX = 460;
}
batch = new SpriteBatch();
circle = new Pixmap(4, 4, Pixmap.Format.RGB888);
texture = new Texture(circle);
circle.setColor(Color.WHITE);
circle.fillCircle(4/2, 4/2, 4/2);
texture.draw(circle, 0, 0);
texture.bind();
position = new Vector2(randX, randY);
//setGravity(gravity, new Vector2(0, -20));
}
public void setGravity(boolean gravity, Vector2 Vgravity){
this.Vgravity = Vgravity;
snowMovement = 60;
}
@Override
public void dispose() {
/*
texture.dispose();
circle.dispose();
batch.dispose();
*/
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void render() {
float deltaTime = Gdx.graphics.getDeltaTime();
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(texture, position.x, position.y);
batch.end();
if (gravity) {
position.y += Vgravity.y * deltaTime;
position.x += snowMovement * j * deltaTime;
j *= -1;
}
}
@Override
public void resize(int arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
}
现在,我想创建一个类来调用“粒子”类,例如100次。但是......我不知道我应该叫谁。我使用这个代码但是,当我运行它时,它只显示一个雪。换句话说,第二个粒子没有读。
public class Emitter extends Particle{
Particle part = new Particle();
Particle part2 = new Particle();
Array<Particle> particle = new Array<Particle>();
public void create(){
part.create();
part.setGravity(true, new Vector2(0, -20));
part2.create();
part2.setGravity(true, new Vector2(0, -20));
particle.add(part);
particle.add(part2);
}
public void render(){
for (int i = 0; i < particle.size; i++) {
Particle num = particle.get(i);
num.render();
}
}
}
“这个课只是举例......”
我不知道我必须在哪里调用它,或者我的“粒子”类问题还是...... 请帮助。
答案 0 :(得分:0)
哦,我很抱歉,但代码有些问题。
public class Particle implements ApplicationListener { ... }
这是你的主要游戏类,而不是粒子。因此,请将您的Particles和Emitters的所有逻辑从此类中取出。在此类中,您将创建一个Emitter对象,该对象将创建许多Particle对象。
为您提供一个大纲:
public class MyGame implements ApplicationListener {
// In here you create an Emitter object and call it's update and
// render logic (within the render() method).
}
public class Emitter {
// In here you define the particles that this emitter contains, ...
}
public class Particle {
// In here you define the particle itself, it's movement logic, if you want
// render logic, ...
}
您在代码中所做的是扩展主应用程序类(ApplicationListener),然后尝试将其实例化2次,但当然您一次只能显示一个应用程序。