编程语言
首页 > 编程语言> > android-AndEngine:动画精灵无法与物理处理程序一起使用

android-AndEngine:动画精灵无法与物理处理程序一起使用

作者:互联网

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

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

                }

屏幕控件可完美地用于动画精灵,但是当我创建物理处理程序时,它不会设置动画.
但是,当我不创建物理处理程序时,它就会动画.
那么为什么在创建物理处理程序时不设置动画?

解决方法:

如果您始终重新启动动画,则动画将仅显示第一个图块.
使用“ 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();
        }
    }
}

标签:andengine,2d,sprite,game-physics,android
来源: https://codeday.me/bug/20191123/2064160.html