AndEngine:动画精灵不使用物理处理程序

时间:2013-07-24 04:35:46

标签: android 2d andengine sprite game-physics

动画精灵不使用物理处理程序执行动画

我正在使用 AndEngine GLES2 AnalogOnScreenControl 来移动精灵。精灵是一个人,所以为了显示腿部运动我已经使它成为一个动画精灵:

final AnimatedSprite person = new AnimatedSprite(personX, personY,
            this.mPersonTextureRegion, vertexBufferObjectManager);
    person.setScaleCenterY(this.mPersonTextureRegion.getHeight());
    person.setScale(2);

为了运动,我正在创建一个物理处理程序:

final PhysicsHandler physicsHandler = new PhysicsHandler(person);
person.registerUpdateHandler(physicsHandler);
scene.attachChild(person);

这是屏幕控制的代码:

    final AnalogOnScreenControl analogOnScreenControl = new AnalogOnScreenControl(
            0, CAMERA_HEIGHT
                    - this.mOnScreenControlBaseTextureRegion.getHeight(),
            this.mCamera, this.mOnScreenControlBaseTextureRegion,
            this.mOnScreenControlKnobTextureRegion, 0.1f, 200,
            this.getVertexBufferObjectManager(),
            new IAnalogOnScreenControlListener() {
                @Override
                public void onControlChange(
                        final BaseOnScreenControl pBaseOnScreenControl,
                        final float pValueX, final float pValueY) {
                     physicsHandler
                     .setVelocity(pValueX * 100, pValueY * 100);
                     person.animate(new long[] { 200, 200, 200 }, 3, 5,
                     false);

                }

屏幕控制对动画精灵完美无缺,但是当我创建物理处理程序时,它没有动画效果。 但是当我不创建物理处理程序时它会动画。 那么为什么我在创建物理处理程序时没有动画呢?

2 个答案:

答案 0 :(得分:2)

如果您一直重新启动动画,您的动画将只显示第一个图块。 使用" isAnimationRunning()"。

检查动画是否正在运行
public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) 
{
    if(person!= null)
    {
        //Move your person
        final Vector2 velocity = Vector2Pool.obtain(pValueX * 10, pValueY * 10);
        person.setLinearVelocity(velocity);
        Vector2Pool.recycle(velocity);
        //Check if person is moving. Don't start a new animation, when your animation is already running
        if((pValueX != 0 || pValueY != 0) && person.isAnimationRunning() == false)
        {
            person.animate(new long[] { 200, 200, 200 },0,2,false);
        } 
        //Stop animation, when there is no movement
        else if(pValueX == 0 && pValueY == 0 && person.isAnimationRunning() == true)
        {
            person.stopAnimation();
        }
    }
}

答案 1 :(得分:1)

想出来!只有当pValueX不为0时,即sprite正在移动时,动画必须启动。

public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
if (pValueX > 0) {
//animate
}

else
//stop animation

 physicsHandler.setVelocity(pValueX * 100, pValueY * 100);
}