1

所以我有以下演员:

package com.asteroids.actors;
import com.asteroids.screens.AbstractScreen;
import com.asteroids.utility.FireworkParticleEffect;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.ui.Image;

public class Meteorites extends Image {

public Rectangle meteorRect;
public Vector2 position;
public float speed;
private float rotation = 300;
private ParticleEffect meteorTrailEffect;
private FireworkParticleEffect explodeMeteoriteEffect;

private Meteorites(AtlasRegion region) {
    super(region);
    position = new Vector2();
    meteorRect = new Rectangle(getX(), getY(), getWidth(), getHeight());
    meteorTrailEffect = new ParticleEffect();
    meteorTrailEffect.load(Gdx.files.internal("data/comet_fire"),
            Gdx.files.internal("data"));
    explodeMeteoriteEffect = new FireworkParticleEffect();
    explodeMeteoriteEffect.load(Gdx.files.internal("data/firework"));

    setOrigin(getWidth() / 2, getHeight() / 2);

    addListener(new InputListener() {
        public boolean touchDown(InputEvent event, float x, float y,
                int pointer, int button) {
            explodeMeteoriteEffect.setPosition(position.x, position.y);
            explodeMeteoriteEffect.start();
            remove();
            return false;
        }
    });
}

/**
 * Factory method to create a {@link Meteorites}.
 */
public static Meteorites create(TextureAtlas textureAtlas) {
    AtlasRegion planetRegion = textureAtlas.findRegion("meteorite");

    return new Meteorites(planetRegion);
}

@Override
public void act(float delta) {
    super.act(delta);
    float x = AbstractScreen.GAME_VIEWPORT_WIDTH / 2 - position.x;
    float y = AbstractScreen.GAME_VIEWPORT_HEIGHT / 2 - position.y;
    double dx = x / Math.sqrt(Math.pow(x, 2) + Math.pow(-y, 2));
    double dy = -y / Math.sqrt(Math.pow(x, 2) + Math.pow(-y, 2));
    position.x = (float) (position.x + dx * speed * delta);
    position.y = (float) (position.y - dy * speed * delta);

    setX(position.x - (getWidth() / 2));
    setY(position.y - (getHeight() / 2));
    meteorRect.setX(position.x - (getWidth() / 2));
    meteorRect.setY(position.y - (getHeight() / 2));
    meteorTrailEffect.setPosition(position.x, position.y);
    rotate(rotation * delta);
}

@Override
public void draw(SpriteBatch batch, float parentAlpha) {
    meteorTrailEffect.draw(batch, Gdx.graphics.getDeltaTime());
    explodeMeteoriteEffect.draw(batch, Gdx.graphics.getDeltaTime());
    super.draw(batch, parentAlpha);
}
}

当演员被触摸时,我开始自定义粒子效果:

addListener(new InputListener() {
        public boolean touchDown(InputEvent event, float x, float y,
                int pointer, int button) {
            explodeMeteoriteEffect.setPosition(position.x, position.y);
            explodeMeteoriteEffect.start();
            remove();
            return false;
        }
    });

问题explodeMeteoriteEffect.start()在这里不起作用,有趣的是,如果我将explodeMeteoriteEffect.start()侦听器放在构造函数之外,它确实起作用。

4

0 回答 0