AndEngineのサンプルプロジェクトにピンチズームとスクロールとロングタップを実装する

 お詫びと訂正。
 AndEngineのサンプルプロジェクト(その4)の GameScene.java の中で

    private Sprite sprite;
     
    @Override
    public void createScene(){
        createBackground();
        Sprite sprite = new Sprite(MainActivity.CAMERA_WIDTH / 2, MainActivity.CAMERA_HEIGHT / 2, resourcesManager.sprite_region, vbom);
        attachChild(sprite);
    }

と、しておりました。メンバ変数の意味がありませんね。次の記事につなげるためにも

    private Sprite sprite;
     
    @Override
    public void createScene(){
        createBackground();
        sprite = new Sprite(MainActivity.CAMERA_WIDTH / 2, MainActivity.CAMERA_HEIGHT / 2, resourcesManager.sprite_region, vbom);
        attachChild(sprite);
    }

このように、直させていただきます。
 
 さて。
 
 予告どおり、というか思い付きでロングタップも含め実装してみました。
 GameScene.java に追記。

package com.chabaori.sample;

import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.ContinuousHoldDetector;
import org.andengine.input.touch.detector.HoldDetector;
import org.andengine.input.touch.detector.HoldDetector.IHoldDetectorListener;
import org.andengine.input.touch.detector.PinchZoomDetector;
import org.andengine.input.touch.detector.PinchZoomDetector.IPinchZoomDetectorListener;
import org.andengine.input.touch.detector.ScrollDetector;
import org.andengine.input.touch.detector.ScrollDetector.IScrollDetectorListener;
import org.andengine.input.touch.detector.SurfaceScrollDetector;

import com.chabaori.sample.SceneManager.SceneType;

public class GameScene extends BaseScene  implements IOnSceneTouchListener, IScrollDetectorListener, IPinchZoomDetectorListener, IHoldDetectorListener{
	private Sprite sprite;
    final static float MIN_ZOOM = 1.0f;
    final static float MAX_ZOOM = 3.0f;
    private SurfaceScrollDetector mScrollDetector;
    private PinchZoomDetector mPinchZoomDetector;
    private float mPinchZoomStartedCameraZoomFactor;
    private boolean isTouchOK = true;
    private boolean isMove;
    private boolean isHold = false;
    private ContinuousHoldDetector continuousHoldDetector;
    private long holdTime = 200;
    private float rotation = 0;
	
	@Override
	public void createScene(){
        setOnSceneTouchListener(this);
        setOnAreaTouchTraversalFrontToBack();
        mScrollDetector = new SurfaceScrollDetector(this);
        mScrollDetector.setTriggerScrollMinimumDistance(15f);
        mPinchZoomDetector = new PinchZoomDetector(this);

        continuousHoldDetector = new ContinuousHoldDetector(this);
        registerUpdateHandler(continuousHoldDetector);
        
        setOnSceneTouchListener(this);
        setTouchAreaBindingOnActionDownEnabled(true);
        isTouchOK = true;

		createBackground();
    	sprite = new Sprite(MainActivity.CAMERA_WIDTH / 2, MainActivity.CAMERA_HEIGHT / 2, resourcesManager.sprite_region, vbom);
    	attachChild(sprite);
	}

	@Override
	public void onBackKeyPressed(){
		System.exit(0);
	}

	@Override
	public SceneType getSceneType(){
		return SceneType.SCENE_GAME;
	}
	
	@Override
	public void disposeScene(){
		background.detachSelf();
		background.dispose();
		sprite.detachSelf();
		sprite.dispose();
		this.detachSelf();
		this.dispose();
	}

    @Override
    public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent){
        if(isTouchOK){
            if(isHold){
                isHold = false;
                continuousHoldDetector.reset();
            }
            continuousHoldDetector.onTouchEvent(pSceneTouchEvent);
            if(!isHold){
                mPinchZoomDetector.onTouchEvent(pSceneTouchEvent);
                
                if(mPinchZoomDetector.isZooming()){
                    mScrollDetector.setEnabled(false);
                    isMove = true;
                }
                else{
                    if (pSceneTouchEvent.isActionMove()){
                        mScrollDetector.setEnabled(true);
                    }
                    else if (pSceneTouchEvent.isActionUp()){
                        if(!isMove){
                            isTouchOK = false;
                            final float zoomValue = 1.0f;
                            camera.setZoomFactor(zoomValue);
                            isTouchOK = true;
                        }
                        isMove = false;
                    }
                    else if (pSceneTouchEvent.isActionOutside()){
                        isMove = false;
                    }
                    mScrollDetector.onTouchEvent(pSceneTouchEvent);
                }
            }
        }
        return true;
    }
 
    @Override
    public void onScrollStarted(final ScrollDetector pScollDetector, final int pPointerID, final float pDistanceX, final float pDistanceY) {
        final float zoomFactor = camera.getZoomFactor();
        camera.offsetCenter(-pDistanceX / zoomFactor, pDistanceY / zoomFactor);
        isMove = true;
    }
 
    @Override
    public void onScroll(final ScrollDetector pScollDetector, final int pPointerID, final float pDistanceX, final float pDistanceY) {
        final float zoomFactor = camera.getZoomFactor();
        camera.offsetCenter(-pDistanceX / zoomFactor, pDistanceY / zoomFactor);
    }
     
    @Override
    public void onScrollFinished(final ScrollDetector pScollDetector, final int pPointerID, final float pDistanceX, final float pDistanceY) {
        final float zoomFactor = camera.getZoomFactor();
        camera.offsetCenter(-pDistanceX / zoomFactor, pDistanceY / zoomFactor);
    }
 
    @Override
    public void onPinchZoomStarted(final PinchZoomDetector pPinchZoomDetector, final TouchEvent pTouchEvent) {
        mPinchZoomStartedCameraZoomFactor = camera.getZoomFactor();
    }
 
    @Override
    public void onPinchZoom(final PinchZoomDetector pPinchZoomDetector, final TouchEvent pTouchEvent, final float pZoomFactor) {
        float zoomValue = mPinchZoomStartedCameraZoomFactor * pZoomFactor;
        if(zoomValue < 1.1) zoomValue = MIN_ZOOM;
        else if(zoomValue > MAX_ZOOM) zoomValue = MAX_ZOOM;
        camera.setZoomFactor(zoomValue);
    }
 
    @Override
    public void onPinchZoomFinished(final PinchZoomDetector pPinchZoomDetector, final TouchEvent pTouchEvent, final float pZoomFactor) {
        float zoomValue = mPinchZoomStartedCameraZoomFactor * pZoomFactor;
        if(zoomValue < 1.1) zoomValue = MIN_ZOOM;
        else if(zoomValue > MAX_ZOOM) zoomValue = MAX_ZOOM;
        camera.setZoomFactor(zoomValue);
    }
    
    @Override
    public void onHoldStarted(HoldDetector pHoldDetector, int pPointerID, float pHoldX, float pHoldY){
        isTouchOK = false;
    }
 
    @Override
    public void onHold(HoldDetector pHoldDetector, long pHoldTimeMilliseconds, int pPointerID, float pHoldX, float pHoldY){
        if(!isHold && pHoldTimeMilliseconds > holdTime){
            isHold = true;
            rotation += 90;
            if(rotation == 360) rotation = 0; 
            sprite.setRotation(rotation);
            isTouchOK = true;
        }
    }
         
    @Override
    public void onHoldFinished(HoldDetector pHoldDetector, long pHoldTimeMilliseconds, int pPointerID, float pHoldX, float pHoldY){
    }
}

 ロングタップ制御外にピンチズームを入れただけなのに複雑になりましたね。
 この他にも、各クラスの BoundCamera を ZoomCamera に直す必要があります。
 MainActivity クラスの onCreateEngineOptions() に camera.setBounds(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); を加えるのもお忘れなく。
 
 Sprite をピンチで3倍までズームアップし、タップで等倍に戻します。ロングタップで90度右回転。
device-2014-06-10-223552 device-2014-06-10-223727
 タップとロングタップの制御が甘いのはご愛嬌。
 
 
 そうそう、認証もらった。

アンドロイダー公認デベロッパー認証