I am currently following some tutorials to get introduced to andEngine and box2d and I would like to drag an object (sprite attached to body), whilst it is bouncing (it is registered to a physics world).
I would like to drag so I can see my character moving. It works only if I comment the code as below (the part where I am registering Physics connector). If I uncomment it, everything will work (e.g. even if I set other properties), except setPosition!! - I am sure the code is entering the ACTION_MOVE case
Question 1: why is this approach not working? - maybe I have to drag the body and not the sprite?
Question 2: how can I refer to the body and make operations in it WITHIN THE onAreaTouched? - I tried many different approaches, but it seams that you have to create the onAreaTouch when declaring the sprite only.
Reference: http://stuartmct.co.uk/2012/07/18/andengine-touch-events-with-sprites-and-shapes/ and AndEngine Book
Sprite sPlayer = new Sprite(CAMERA_WIDTH / 2, CAMERA_HEIGHT / 2,
playerTextureRegion,
this.mEngine.getVertexBufferObjectManager()) {
@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
Log.i(this.getClass().getName(), "area touched");
switch (pSceneTouchEvent.getAction()) {
case TouchEvent.ACTION_DOWN:
break;
case TouchEvent.ACTION_UP:
break;
case TouchEvent.ACTION_MOVE:
this.setPosition(pSceneTouchEvent.getX() - this.getWidth()
/ 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
break;
}
return true;
}
};
FixtureDef PLAYER_FIX = PhysicsFactory.createFixtureDef(10.0f, 0.75f,
0.0f);
// apply all forces, collisions to body - Dynamic
Body body = PhysicsFactory.createCircleBody(physicsWorld, sPlayer,
BodyType.DynamicBody, PLAYER_FIX);
The following code does not let me setPosition of Sprite:
/*
// relate to collisions after adding to scene
physicsWorld.registerPhysicsConnector(new PhysicsConnector(sPlayer,
body, true, true));
*/
this.scene.registerTouchArea(sPlayer);
this.scene.setTouchAreaBindingOnActionDownEnabled(true);
this.scene.attachChild(sPlayer);