Shihab Uddin
Shihab Uddin

Reputation: 6931

move a body to correct direction when swipe

I wanna move a physics body(Duck) to accurate direction when user swipe the body. I can detect the scene swipe event. But how to connect with my body, when swiped.

My code: In my game Scene Class >>

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

Now my object don't move to any direction. I got the IOS code implementation here. can some body help to do this in ANDROID.

Upvotes: 0

Views: 64

Answers (1)

Izu
Izu

Reputation: 235

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

Upvotes: 1

Related Questions