0

我想在 AndEngine 中画一条线,其效果类似于 Fruit Ninja 或 Veggie Samurai。谁能帮我?并给出示例代码?

4

2 回答 2

1

也许,我迟到了,但希望这对其他人有帮助。

    @Override
    public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {

        final float touchX = pSceneTouchEvent.getX();
        final float touchY = pSceneTouchEvent.getY();

        if (pSceneTouchEvent.isActionDown()) {
            initTrail(touchX, touchY);
            swipeGestureSprite = addSwipeSprite(touchX, touchY);
        } else if (pSceneTouchEvent.isActionMove()) {

            moveTrail(pSceneTouchEvent.getX(), pSceneTouchEvent.getY(), 0);

            if (swipeGestureSprite != null) {
                swipeGestureSprite.setX(touchX);
                swipeGestureSprite.setY(touchY);
            }
        } else if (pSceneTouchEvent.isActionUp()) {

            mScene.detachChild(this.particleSystem);

            if (swipeGestureSprite != null) {
                this.mScene.detachChild(swipeGestureSprite);
                swipeGestureSprite = null;
            }

            return true;
        }

        return false;
    }




    private void initTrail(float mX, float mY) {
        this.particleEmitter = new PointParticleEmitter(mX, mY);
        this.particleSystem = new SpriteParticleSystem(particleEmitter, 1000, 1000, 5000, this.mRibbon, this.getVertexBufferObjectManager());
        particleSystem.addParticleInitializer(new BlendFunctionParticleInitializer<Sprite>(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE));
        particleSystem.addParticleInitializer(new ExpireParticleInitializer<Sprite>(1));
        particleSystem.addParticleModifier(new ScaleParticleModifier<Sprite>(0, 1, 1, 0f));
        particleSystem.addParticleModifier(new AlphaParticleModifier<Sprite>(0, 1, 1, 0));
        this.mScene.attachChild(particleSystem);
    }


private void moveTrail(float trailX, float trailY, int count) {
        if (particleEmitter == null) {
            initTrail(trailX, trailY);
        }

        particleEmitter.setCenter(trailX, trailY);
    }
于 2014-02-12T10:13:14.240 回答
0

我认为你必须使用点粒子效果并使用画线算法来创建点之间的线。使用粒子绘制的线代表更多的现实,粒子也代表动态的方式,因此您可以破坏它。进一步你可以问。编辑:对于粒子生成,您可以使用此代码

public void generateParticles(float pX, float pY) {

    pointParticleEmtitter = new PointParticleEmitter(pX, pY);

    particleSystem = new ParticleSystem(pointParticleEmtitter, maxRate,
            minRate, maxParticles, mParticleTextureRegion.deepCopy());

    particleSystem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
    particleSystem.addParticleInitializer(new ColorInitializer(0.8f, 0.43f,
            0.2f));
    particleSystem.addParticleModifier(new AlphaModifier(1, 0, 0, 0.25f));
    particleSystem.addParticleModifier(new ExpireModifier(0.25f));

    gameObject.getScene().attachChild(particleSystem);

}

对于线算法,你可以使用这个

public void drawLine() {
    int x;
    float y, m;

    x = (int) previousX;
    y = (int) previousY;

    m = (currentY - previousY) / (currentX - previousX);

    if (Math.round(previousX) == Math.round(currentX)) {
        if (previousY < currentY) {
            for (y = (int) previousY; y < currentY; ++y)
                pointParticleEmtitter.setCenter(previousX, y);
        } else {
            for (y = (int) previousY; y > currentY; --y)
                pointParticleEmtitter.setCenter(previousX, y);
        }
    } else {
        if (previousX < currentX) {
            for (x = (int) previousX; x < currentX; ++x) {
                y = m * (x - previousX) + previousY;
                pointParticleEmtitter.setCenter(x, y);
            }
        } else if (previousX > currentX) {
            for (x = (int) previousX; x > currentX; --x) {
                y = m * (x - currentX) + currentY;
                pointParticleEmtitter.setCenter(x, y);
            }
        }
    }
}
于 2012-06-05T07:37:55.583 回答