2013-10-16 73 views
0

我正在使用AndEngine并尝试检测游戏中的轻扫手势。在引擎中有一个类(SurfaceGestureDetector.java),但我不知道如何使用它。当我尝试在onCreateScene()方法中实例化时,它会抛出异常。如果有任何方法或内部类为它提供或任何其他方法来检测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); 
      } 
     }); 
    } 
+0

我试过下面的链接,但不知道如何在我的活动中使用它.http://stackoverflow.com/questions/17105657/swipe-scenes-in-andengine – puneetbhardwaj

回答

0

您的代码格式不正确,但使用SurfaceGestureDetector您需要对其进行子类化并实施检测方法。然后你通过onSceneTouchEvent到检测器是这样的:

private MySubclassedSurfaceGestureDectetor gestureDetector; 

@Override 
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) { 
    return gestureDetector.onManagedTouchEvent(pSceneTouchEvent); 
} 

它将magicly(不是magicly如果你看看源:P)检测的手势为您服务。