【问题标题】:AndEngine: animated sprite not working with physics handlerAndEngine:动画精灵不与物理处理程序一起使用
【发布时间】:2013-07-24 04:35:45
【问题描述】:

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

我正在使用 AndEngine GLES2AnalogOnScreenControl 来移动精灵。精灵是一个人,因此为了显示腿部运动,我将其制作为动画精灵:

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);

                }

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

【问题讨论】:

  • onControlChange 调用过于频繁,并且您的下一个 person.animate() 调用将重置之前的 person.animate(),因此您只能看到动画精灵的第一帧。尝试有条件地调用你的 person.animate()。
  • 我应该在哪里添加这个条件?

标签: android 2d andengine sprite game-physics


【解决方案1】:

如果您一直重新启动动画,您的动画只会显示第一个图块。 使用“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();
        }
    }
}

【讨论】:

    【解决方案2】:

    想通了!只有当 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多