Android 在andengine中检测水平和垂直滑动手势

Android 在andengine中检测水平和垂直滑动手势,android,andengine,swipe,gesture,swipe-gesture,Android,Andengine,Swipe,Gesture,Swipe Gesture,我正在使用AndEngine并尝试检测游戏中的刷卡手势。在andengine中有一个类(SurfaceGestureDetector.java),但我不知道如何使用它。当我尝试在onCreateSecene()方法中实例化时,它会抛出异常。是否为它提供了任何方法或内部类,或者提供了任何其他方法来检测andengine中的滑动。这是我的代码 public class AETopDownBackground extends SimpleBaseGameActivity implements IOnS

我正在使用AndEngine并尝试检测游戏中的刷卡手势。在andengine中有一个类(SurfaceGestureDetector.java),但我不知道如何使用它。当我尝试在onCreateSecene()方法中实例化时,它会抛出异常。是否为它提供了任何方法或内部类,或者提供了任何其他方法来检测andengine中的滑动。这是我的代码

public class AETopDownBackground extends SimpleBaseGameActivity implements IOnSceneTouchListener{

        private int CAMERA_WIDTH=800;
        private int CAMERA_HEIGHT=480;
        private Camera mCamera;
        private Scene mScene;
        private BuildableBitmapTextureAtlas atlas,txchrAtlas;
        private TextureRegion trHill;
        //private Sprite mSpriteHill;
        private AnimatedSprite mSpriteMan;
        private TiledTextureRegion trTiledMan;

        @Override
        public EngineOptions onCreateEngineOptions() {
            // TODO Auto-generated method stub
            mCamera=new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
            return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera); 

        }

        @Override
        protected void onCreateResources() {
            // TODO Auto-generated method stub


            BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/"); 
            atlas= new BuildableBitmapTextureAtlas(getTextureManager(), 800, 1300);
            trHill=BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlas, this, "road_bg.png"); 


            try {
                atlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
            } catch (TextureAtlasBuilderException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            atlas.load();


            txchrAtlas= new BuildableBitmapTextureAtlas(getTextureManager(), 256, 92,TextureOptions.BILINEAR);
            trTiledMan= BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(txchrAtlas, this, "player_forword.png",4, 1);

            try {
                txchrAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
            } catch (TextureAtlasBuilderException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            txchrAtlas.load();

        }

        @Override
        protected Scene onCreateScene() {
            // TODO Auto-generated method stub
            this.mEngine.registerUpdateHandler(new FPSLogger());
            mScene = new Scene();       
            mScene.setBackground(new Background(Color.CYAN));

            mScene.setOnSceneTouchListener(this); 
            //gdetectorl= new MySurfaceSwipeDetector(getApplicationContext());

            //final float textureHeight = trHill.getHeight();
            /* Create the hill which will appear to be the furthest
            * into the distance. This Sprite will be placed higher than the
            * rest in order to retain visibility of it */
            Sprite hillFurthest = new Sprite(0, 0, trHill,
            mEngine.getVertexBufferObjectManager());


            //ParallaxBackground background = new ParallaxBackground(0.3f, 0.3f,0.9f) {
            AutoParallaxBackground background = new AutoParallaxBackground(0.3f,0.3f, 0.9f, 50){
                /* We'll use these values to calculate the parallax value of the
                background */
                float cameraPreviousY = 0;
                float parallaYValueOffset = 0;
                /* onUpdates to the background, we need to calculate new
                * parallax values in order to apply movement to the background
                * objects (the hills in this case) */
                @Override
                public void onUpdate(float pSecondsElapsed) {
                /* Obtain the camera's current center X value */
                final float cameraCurrentY = mCamera.getCenterY();
                /* If the camera's position has changed since last
                * update... */
                if (cameraPreviousY != cameraCurrentY) {//cameraPreviousX != cameraCurrentX
                /* Calculate the new parallax value offset by
                * subtracting the previous update's camera x coordinate
                * from the current update's camera x coordinate */
                parallaYValueOffset += cameraCurrentY - cameraPreviousY;
                    //parallaxValueOffset=parallaxValueOffset+10;
                /* Apply the parallax value offset to the background, which
                * will in-turn offset the positions of entities attached
                * to the background */ 
                this.setParallaxValue(parallaYValueOffset);
                System.out.println("Camera parallaxValueOffset:"+parallaYValueOffset); 

                //this.setParallaxValue(0);
                /* Update the previous camera X since we're finished with
                this
                * update */
                cameraPreviousY = cameraCurrentY;
                }
            //  this.setParallaxValue(2);
                super.onUpdate(pSecondsElapsed);
                }
            };
                    //negative value in this method indicates movement.To change the direction of movement use (+)positive  value
                    background.attachParallaxEntity(new ParallaxEntity(10,hillFurthest));



                    /* Set & Enabled the background */
                    mScene.setBackground(background);
                    mScene.setBackgroundEnabled(true);


                    mSpriteMan = new AnimatedSprite(310, 250, trTiledMan, getVertexBufferObjectManager());
                    mSpriteMan.setScale(3); 
                    mSpriteMan.animate(100);
                    mScene.attachChild(mSpriteMan);         



            return mScene;
        }

        @Override
        public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
            // TODO Auto-generated method stub
            if(pSceneTouchEvent.isActionUp()){
                //Toast.makeText(this, "touched", Toast.LENGTH_SHORT).show();
                mSpriteMan.setPosition(pSceneTouchEvent.getX(), pSceneTouchEvent.getY()); 
            }


            return true;
        }

    }                   @Override
                    protected boolean onSwipeLeft() {
                        // TODO Auto-generated method stub
                        onSurfaceGesture("test"); 
                        return true;
                    }

                    @Override
                    protected boolean onSwipeDown() {
                        // TODO Auto-generated method stub
                        onSurfaceGesture("test"); 
                        return true;
                    }

                    @Override
                    protected boolean onSingleTap() {
                        // TODO Auto-generated method stub
                        onSurfaceGesture("test"); 
                        return true;
                    }

                    @Override
                    protected boolean onDoubleTap() {
                        // TODO Auto-generated method stub
                        onSurfaceGesture("test"); 
                        return true;
                    }
                };                  
                    this.mSGDA.setEnabled(true);



        return mScene;
    }

    @Override
    public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
        // TODO Auto-generated method stub

        if(pSceneTouchEvent.isActionUp()){
            //Toast.makeText(this, "touched", Toast.LENGTH_SHORT).show();
            //mSpriteMan.setPosition(pSceneTouchEvent.getX(), pSceneTouchEvent.getY()); 


        }


        return true;
    }


    private void onSurfaceGesture(final String str){ 

        try {
            Looper.prepare();
        } catch (Exception e) { 
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        new Handler().post(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                System.out.println("Action is :"+str); 
            }
        });
    }
公共类AETopDownBackground扩展SimpleBaseGameActivity实现OnSceneTouchListener{
专用int摄像机_宽度=800;
私用内窥镜高度=480;
私人摄像机麦卡梅拉;
私密场景;
私人可建造的BitmapTextureAtlas地图集,txchrAtlas;
特希尔私人酒店;
//私人斯普里特山;
私人动画精灵mSpriteMan;
私人平铺纺织区trTiledMan;
@凌驾
public EngineOptions onCreateEngineOptions(){
//TODO自动生成的方法存根
mCamera=新摄像头(0,0,摄像头宽度,摄像头高度);
返回新的EngineOptions(true,ScreenOrientation.横向固定,新比率解决方案策略(摄像头宽度,摄像头高度),mCamera);
}
@凌驾
受保护的void onCreateResources(){
//TODO自动生成的方法存根
BitmapTextureLastTextureRegionFactory.setAssetBasePath(“gfx/”);
atlas=新的可构建BitmapTextureAtlas(getTextureManager(),8001300);
trHill=BitMapTextureAtlastTextureRegionFactory.createFromAsset(atlas,这是“road_bg.png”);
试一试{
atlas.build(新的BlackPawnTextureAtlasBuilder(0,0,0));
}捕捉(纹理LasBuilderException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
atlas.load();
txchrAtlas=newbuildableBitmapTextureAtlas(getTextureManager(),256,92,TextureOptions.BILINEAR);
trTiledMan=BitmapTextureLastTextureRegionFactory.createTiledFromAsset(txchrAtlas,这是“player\u forword.png”,4,1);
试一试{
构建(新的BlackPawnTextureAtlasBuilder(0,0,0));
}捕捉(纹理LasBuilderException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
} 
txchrAtlas.load();
}
@凌驾
受保护的场景onCreateSecene(){
//TODO自动生成的方法存根
this.mEngine.registerUpdateHandler(新的FPSLogger());
mScene=新场景();
mScene.setBackground(新背景色(青色));
mScene.setOnSceneTouchListener(此);
//gdetector=newmysurfaceswipedtector(getApplicationContext());
//最终浮点纹理高度=trHill.getHeight();
/*创建看起来最远的山
*到远处。这个精灵将被放置在高于
*静止以保持其可见性*/
雪碧山丘最远=新雪碧(0,0,trHill,
mEngine.getVertexBufferObjectManager());
//视差背景=新视差背景(0.3f、0.3f、0.9f){
AutoParallaxBackground背景=新的AutoParallaxBackground(0.3f、0.3f、0.9f、50){
/*我们将使用这些值来计算图像的视差值
背景*/
float cameraprevousy=0;
浮动视差值偏移=0;
/*更新到后台后,我们需要计算新的
*视差值,以便将移动应用于背景
*对象(本例中为山丘)*/
@凌驾
更新时的公共无效(浮点psecondsappeased){
/*获取相机的当前中心X值*/
最终浮点cameraCurrentY=mCamera.getCenterY();
/*如果相机的位置自上次更改后
*更新*/
如果(cameraPreviousY!=cameraCurrentY){//cameraPreviousX!=cameraCurrentX
/*通过偏移计算新视差值
*减去上一次更新的摄影机x坐标
*从当前更新的摄影机x坐标*/
视差值偏移+=摄像机当前-摄像机先前;
//视差值偏移=视差值偏移+10;
/*将视差值偏移应用于背景,这将
*将反过来抵消附着实体的位置
*到后台*/
此.setParallaxValue(视差值偏移);
System.out.println(“摄像机视差值偏移量:“+视差值偏移量”);
//此值为。设置视差值(0);
/*更新之前的摄影机X,因为我们已完成
这
*更新*/
cameraPreviousY=cameraCurrentY;
}
//此设置视差值(2);
super.onUpdate(psecondsappeased);
}
};
//此方法中的负值表示移动。若要更改移动方向,请使用(+)正值
背景:附加视差(新视差(10,希尔最远));
/*设置并启用背景*/
mScene.挫折背景(背景);
mScene.setBackgroundEnabled(真);
mSpriteMan=newanimatedsprite(310250,trTiledMan,getVertexBufferObjectManager());
mSpriteMan.setScale(3);
制作动画(100);
mScene.attachChild(mSpriteMan);
返回mScene;
}
@凌驾
公共布尔onSceneTouchEvent(场景pScene,到