3

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

我正在使用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);

                }

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

4

2 回答 2

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();
        }
    }
}
于 2015-01-07T20:53:59.380 回答
1

弄清楚了!只有当 pValueX 不为 0 时,动画才必须开始,即,如果精灵正在移动。

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);
}
于 2013-07-24T09:48:08.883 回答