使用模拟控制器旋转精灵



你好,我正在使用andengine开发一个游戏,现在我希望我的精灵可以用OnScreenAnalogController旋转。我已经初始化了它,但现在我不知道如何完成其余的工作。任何示例代码或任何内容将不胜感激。

提前谢谢。

P.S 旋转应该围绕精灵的轴。当我松开控制器时,我希望角色朝向它旋转的方向,而不是最初的方向。

对于精灵旋转,我们覆盖了 applyRotation() 方法 ->

Sprite sprite = new Sprite(0, 0, textureRegionForMySprite, getVertexBufferObjectManager()){
    @Override
    protected void applyRotation(GLState pGLState) {
        pGLState.rotateModelViewGLMatrixf(this.mRotation, 0, 1, 0);
    }
};

我有点使用此代码找到了解决方案

    mAnalogController = new AnalogOnScreenControl(90, cameraHeight - 130, mCamera, mControllerTextureRegion, mKnobTextureRegion, 0.01f, getVertexBufferObjectManager(), new IAnalogOnScreenControlListener(){
        @Override
        public void onControlChange(
                BaseOnScreenControl pBaseOnScreenControl, float pValueX,
                float pValueY) {
            //rect.registerEntityModifier(new RotationByModifier(0.5f, MathUtils.radToDeg((float) Math.atan2(-pValueX, pValueY))));
            rect.registerEntityModifier(new RotationModifier(0.1f, rect.getRotation(), MathUtils.radToDeg((float) Math.atan2(pValueX, -pValueY))));
        }
        @Override
        public void onControlClick(
                AnalogOnScreenControl pAnalogOnScreenControl) {
            // TODO Auto-generated method stub
        }
    });

唯一的问题是当我释放控制器时,它会回到初始位置并旋转,这不是我想要的。关于我该怎么做的任何想法?

最新更新