Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

add this code in onCreateEngineOptions()

final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
        engineOptions.getTouchOptions().setNeedsMultiTouch(true);

        if(MultiTouch.isSupported(this)) {
            if(MultiTouch.isSupportedDistinct(this)) {
                Toast.makeText(this, "MultiTouch detected --> Both controls will work properly!", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "MultiTouch detected, but your device has problems distinguishing between fingers.\n\nControls are placed at different vertical locations.", Toast.LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(this, "Sorry your device does NOT support MultiTouch!\n\n(Falling back to SingleTouch.)\n\nControls are placed at different vertical locations.", Toast.LENGTH_LONG).show();
        }
share|improve this answer
add comment

I think first you have to look example provided by the AndEngine that is about pinch zoom functionality. Only this example is capable to provide enough guidance to achieve zoom in/out facility.

http://code.google.com/p/andengineexamples/source/browse/src/org/anddev/andengine/examples/PinchZoomExample.java

Also you have to add another extension for multi-touch support to you project.

https://github.com/nicolasgramlich

Above link contain information about all the andengine extension, you have to pick that you need.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.