控制精灵不完全工作


public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
            final Body carBody = CityRacerActivity.this.mCarBody;
            final Vector2 velocity = Vector2Pool.obtain(pValueX * 5, pValueY * 5);
            carBody.setLinearVelocity(velocity);
            Vector2Pool.recycle(velocity);
            final float rotationInRad = (float)Math.atan2(-pValueX, pValueY);
            carBody.setTransform(carBody.getWorldCenter(), rotationInRad);
            CityRacerActivity.this.mCar.setRotation(MathUtils.radToDeg(rotationInRad));
        }

所以这部分有效。

OnControlChange 将精灵移动到正确的方向,但当我松开控件时,它似乎每次都会朝上移动车辆。

我在安卓上使用和引擎。

我的代码基于赛车游戏活动示例,但此错误似乎已经存在于示例本身中。

        public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
            final Body carBody = CityRacerActivity.this.mCarBody;
            final float rotationInRad = (float)Math.atan2(-pValueX, pValueY);
            if ((pValueX == 0) && (pValueY == 0))
            {
                    //Don't turn the body/sprite of the car

            }else
            {
                    carBody.setTransform(carBody.getWorldCenter(), rotationInRad);
                    //turn the car body in the direction of movement
                    CityRacerActivity.this.mCar.setRotation(MathUtils.radToDeg(rotationInRad));
            }
            //set the velocity
            final Vector2 velocity = Vector2Pool.obtain(pValueX * 5, pValueY * 5);
            carBody.setLinearVelocity(velocity);
            Vector2Pool.recycle(velocity);
        }

就像我想要的那样工作,但我不确定这是否是我应该处理它的方式。

最新更新