滑动时移动身体以纠正方向

时间:2014-05-11 06:34:38

标签: android box2d andengine

当用户滑动身体时,我想将物理身体(鸭子)移动到准确的方向。我可以检测到场景滑动事件。但刷卡时如何与我的身体连接。

我的代码:在我的游戏场景类>>

    // called in scene constructor


    private void addDuck(final float pX, final float pY) {

            duck = new Duck(pX, pY, resourcesManager.mDuckTextureRegion,
                    vertexBufferObjectManager) {
           @Override
            public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
                    float pTouchAreaLocalX, float pTouchAreaLocalY) {


                this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2,
                        pSceneTouchEvent.getY() - this.getHeight() / 2);

                Log.e("onAreaTouched", "Inside Duck");

                return true;
            }

            };

            body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, duck,
                    BodyType.DynamicBody, objectFixtureDef);
            duck.setPhysicsWorld(mPhysicsWorld, body);
            registerTouchArea(duck);
            getChildByIndex(SECOND_LAYER).attachChild(duck);
            duckList.add(duck);
            body.setGravityScale(0);
        }

现在我的目标没有向任何方向移动。我得到了IOS代码实现here。有些人可以帮助在ANDROID中做到这一点。

1 个答案:

答案 0 :(得分:1)

   @Override
                public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
                        float pTouchAreaLocalX, float pTouchAreaLocalY) {
                                        if (pSceneTouchEvent.isActionMove()) {
                      this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2,
                            pSceneTouchEvent.getY() - this.getHeight() / 2);
                        return true;
                    }
                    return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX,
                            pTouchAreaLocalY);
                }
            };