Detect multitouch (two fingers touch) on a sprite to apply pinch zoom behaviour

Posted by Tahreem on Game Development See other posts from Game Development or by Tahreem
Published on 2012-12-07T09:50:06Z Indexed on 2012/12/07 11:20 UTC
Read the original article Hit count: 303

Filed under:
|
|

I am using andengine, want to move, zoom and rotate multiple sprites individually on a scene. I have achieved "move" but for pinch zoom i am unable to get the event to two fingers' touch. Below is the code:

public class Main extends SimpleBaseGameActivity {

private Camera camera;
private BitmapTextureAtlas mBitmapTextureAtlas;
private ITextureRegion mFaceTextureRegion;
private ITextureRegion mFaceTextureRegion2;
Sprite face2;

private static final int CAMERA_WIDTH = 800;
private static final int CAMERA_HEIGHT = 480;

@Override
public EngineOptions onCreateEngineOptions() {
    camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
    EngineOptions engineOptions = new EngineOptions(true,
            ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(
                    CAMERA_WIDTH, CAMERA_HEIGHT), camera);

    return engineOptions;
}

@Override
protected void onCreateResources() {
    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

    this.mBitmapTextureAtlas = new BitmapTextureAtlas(
            this.getTextureManager(), 1024, 1600, TextureOptions.NEAREST);
BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas,
    // this, "ui_ball_1.png", 0, 0, 1, 1),
    // this.getVertexBufferObjectManager());
    this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory
            .createFromAsset(this.mBitmapTextureAtlas, this,
                    "ui_ball_1.png", 0, 0);
    this.mFaceTextureRegion2 = BitmapTextureAtlasTextureRegionFactory
            .createFromAsset(this.mBitmapTextureAtlas, this,
                    "ui_ball_1.png", 0, 0);
    this.mBitmapTextureAtlas.load();
this.mEngine.getTextureManager().loadTexture(this.mBitmapTextureAtlas);

}

@Override
protected Scene onCreateScene() {

    this.mEngine.registerUpdateHandler(new FPSLogger());

    final Scene scene = new Scene();
    scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));

    final float centerX = (CAMERA_WIDTH - this.mFaceTextureRegion
            .getWidth()) / 2;
    final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion
            .getHeight()) / 2;

    final Sprite face = new Sprite(centerX, centerY,
            this.mFaceTextureRegion, this.getVertexBufferObjectManager()) {
        @Override
        public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
                final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
            this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2,
                    pSceneTouchEvent.getY() - this.getHeight() / 2);
            return true;
        }
    };
    face.setScale(2);
    scene.attachChild(face);
    scene.registerTouchArea(face);

     face2 = new Sprite(200, 200, this.mFaceTextureRegion2,
            this.getVertexBufferObjectManager()) {
        @Override
        public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
                final float pTouchAreaLocalX, final float pTouchAreaLocalY) {

            switch(pSceneTouchEvent.getAction()){
            case TouchEvent.ACTION_DOWN:


                int count = pSceneTouchEvent.getMotionEvent().getPointerCount() ;

                for(int i= 0; i <count; i++)
                {
                    int id = pSceneTouchEvent.getMotionEvent().getPointerId(i);
                }
                    break;
            case TouchEvent.ACTION_MOVE:

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

                    break;

            case TouchEvent.ACTION_UP:

                    break;

            }
            return true;
            }
        };

    face2.setScale(2);
    scene.attachChild(face2);
    scene.registerTouchArea(face2);

    scene.setTouchAreaBindingOnActionDownEnabled(true);
    return scene;
}

}

This line

int count = pSceneTouchEvent.getMotionEvent().getPointerCount() ;

should set 2 to the count variable if i touch the sprite with to fingers, then i can apply zooming functionality (setScale method) on the sprite by getting the distance between the coordinates of two fingers. Can anyone help me? why it does not detect two fingers? and without this how can i zoom the sprite on pinch of two fingers?

I am very new to game development, any help would be appreciated. Thanks in advance.

© Game Development or respective owner

Related posts about sprites

Related posts about andengine