2014-04-21 21 views
0

我的代碼如下,使用ANDEngine假設在SimpleBaseGameActivity上正常工作,其中2 Sprite將動畫和移動。但是當它聲明一個對象時,圖像會變形。我甚至沒有使用這個對象。 By comment out out聲明surfaceGestureDetector對象,扭曲整個圖像

gDetector = new SurfaceGestureDetector(this){...} 

一切都正常。有人可以建議出了什麼問題嗎?謝謝!

public class MainActivity extends SimpleBaseGameActivity 
{ 

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

    private BitmapTextureAtlas yourTexture; 
    private TiledTextureRegion yourTextureRegion; 
    private SurfaceGestureDetector gDetector; 


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

     return engineOptions; 
    } 



    @Override 
    public void onCreateResources() 
    { 
     loadGraphics(); 

    } 

    private void loadGraphics() 
    { 
     BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/"); 
     yourTexture = new BitmapTextureAtlas(getTextureManager(), 50, 200, TextureOptions.BILINEAR); 
     yourTextureRegion =BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(yourTexture, this, "snowman_animate.png", 0, 0, 1, 4); 
     yourTexture.load();  
    } 


    @Override 
    public Scene onCreateScene() { 
    Scene scene = new Scene(); 
    scene.setBackground(new Background(0.6004f, 0.6274f, 0.8784f)); 

    gDetector = new SurfaceGestureDetector(this){ 
      @Override 
      protected boolean onSwipeUp() { 
       return false; 
      } 

      @Override 
      protected boolean onSwipeRight() { 
       return false; 
      } 

      @Override 
      protected boolean onSwipeLeft() { 
       return false; 
      } 

      @Override 
      protected boolean onSwipeDown() { 
       return false; 
      } 

      @Override 
      protected boolean onSingleTap() { 
       return false; 
      } 

      @Override 
      protected boolean onDoubleTap() { 
       return false; 
      } 
     }; 


     MySprite yourSprite = new MySprite(200, 400, yourTextureRegion, this.getVertexBufferObjectManager()); 
     MySprite yourSprite2 = new MySprite(300, 200, yourTextureRegion, this.getVertexBufferObjectManager()); 
     long[] frameDurration = {250, 250, 250, 250}; 
     yourSprite.animate(frameDurration); 
     yourSprite2.animate(frameDurration); 

     scene.attachChild(yourSprite); 
     scene.attachChild(yourSprite2); 

     return scene; 

    } 

    private static class MySprite extends AnimatedSprite { 
     private final PhysicsHandler mPhysicsHandler; 

     public MySprite 
      (final float pX, final float pY, final TiledTextureRegion pTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager) { 
      super(pX, pY, pTextureRegion, pVertexBufferObjectManager); 
      this.mPhysicsHandler = new PhysicsHandler(this); 
      this.registerUpdateHandler(this.mPhysicsHandler); 
      this.mPhysicsHandler.setVelocity(0, 120); 
     } 

     @Override 
     protected void onManagedUpdate(final float pSecondsElapsed) { 

      if(this.mY < 0) { 
       this.mPhysicsHandler.setVelocityY(120); 
      } else if(this.mY + this.getHeight() > CAMERA_HEIGHT) { 
       this.mPhysicsHandler.setVelocityY(-120); 
      } 

      super.onManagedUpdate(pSecondsElapsed); 
     } 
    } 

} 

回答

0

找到解決我的問題的方法。雖然我不明白爲什麼。我需要在onCreate而不是在onCreateScene中聲明它,如下所示。

public void onCreate(final Bundle pSavedInstanceState) { 
    super.onCreate(pSavedInstanceState); 
    gDetector = new SurfaceGestureDetector(this){ 
     @Override 
     protected boolean onSwipeUp() { 
      return false; 
     } 

     @Override 
     protected boolean onSwipeRight() { 
      return false; 
     } 

     @Override 
     protected boolean onSwipeLeft() { 
      return false; 
     } 

     @Override 
     protected boolean onSwipeDown() { 
      return false; 
     } 

     @Override 
     protected boolean onSingleTap() { 
      return false; 
     } 

     @Override 
     protected boolean onDoubleTap() { 
      return false; 
     } 
    }; 

}