2013-05-30 103 views
1

我正在使用Andengine開發我的遊戲。我在我的遊戲中使用了AnalogOnScreenControl。通過一些教程的幫助,我可以使用此控件移動我的精靈。並且爲此使用了以下代碼。andengine中的AnalogOnScreenControl偵聽器

final AnalogOnScreenControl analogOnScreenControl = new AnalogOnScreenControl(100, 100, this.camera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, 200, this.getVertexBufferObjectManager(), new IAnalogOnScreenControlListener() { 
    @Override 
    public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) { 
     physicsHandler.setVelocity(pValueX * 100, pValueY * 100); 

     } 

    @Override 
    public void onControlClick(final AnalogOnScreenControl pAnalogOnScreenControl) { 
     player.registerEntityModifier(new SequenceEntityModifier(new ScaleModifier(0.25f, 1, 1.5f), new ScaleModifier(0.25f, 1.5f, 1))); 
    } 
}); 

我使用的是平鋪圖像和動畫使用精靈,

player.animate(new long[]{200, 200, 200}, 3, 5, true); 

。我想根據從AnalogOnScreenControl.Means用戶輸入指定動畫精靈的順序,當用戶進行AnalogOnScreenControl中的用戶輸入我需要聽它並根據它設置動畫的順序。我可以檢測模擬搖桿在哪一側移動?

回答

2

我已發現從AndEngine forum.Here溶液是代碼,

public class TestActivity extends BaseGameActivity { 

    private TMXTiledMap mTMXTiledMap; 
    private BoundCamera mBoundChaseCamera; 

    private static final int CAMERA_WIDTH = 480; 
private static final int CAMERA_HEIGHT = 320; 

    private Texture mTexture; 
    private Texture mTexturePlayer; 
    private TiledTextureRegion mPlayerTextureRegion; 
    private Texture mOnScreenControlTexture; 
    private TextureRegion mOnScreenControlBaseTextureRegion; 
    private TextureRegion mOnScreenControlKnobTextureRegion; 

    private DigitalOnScreenControl mDigitalOnScreenControl; 

    private enum PlayerDirection{ 
      NONE, 
      UP, 
      DOWN, 
      LEFT, 
      RIGHT 
    } 
    private PlayerDirection playerDirection = PlayerDirection.DOWN; 


    @Override 
    public Engine onLoadEngine() { 
      this.mBoundChaseCamera = new BoundCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); 
      return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mBoundChaseCamera)); 
    } 

    @Override 
    public void onLoadResources() { 
      TextureRegionFactory.setAssetBasePath("gfx/"); 

      this.mTexture = new Texture(32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA); 
      this.mTexturePlayer = new Texture(128, 128, TextureOptions.DEFAULT); 

      this.mOnScreenControlTexture = new Texture(256, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA); 
      this.mOnScreenControlBaseTextureRegion = TextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_base.png", 0, 0); 
      this.mOnScreenControlKnobTextureRegion = TextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_knob.png", 128, 0); 

      this.mPlayerTextureRegion = TextureRegionFactory.createTiledFromAsset(this.mTexturePlayer, this, "hero.png", 0, 0, 3, 4); 

      this.mEngine.getTextureManager().loadTextures(this.mTexture, this.mOnScreenControlTexture); 
      this.mEngine.getTextureManager().loadTexture(this.mTexturePlayer); 
    } 

    @Override 
    public Scene onLoadScene() { 
      this.mEngine.registerUpdateHandler(new FPSLogger()); 

      final Scene scene = new Scene(); 

      try { 
        final TMXLoader tmxLoader = new TMXLoader(this, this.mEngine.getTextureManager(), TextureOptions.BILINEAR_PREMULTIPLYALPHA, null); 
        this.mTMXTiledMap = tmxLoader.loadFromAsset(this, "tmx/test.tmx"); 

      } catch (final TMXLoadException tmxle) { 
        Debug.e(tmxle); 
      } 

      for (int i = 0; i < this.mTMXTiledMap.getTMXLayers().size(); i++){ 
        TMXLayer layer = this.mTMXTiledMap.getTMXLayers().get(i); 
        scene.attachChild(layer); 
      } 

      /* Make the camera not exceed the bounds of the TMXEntity. */ 
      final TMXLayer tmxLayer = this.mTMXTiledMap.getTMXLayers().get(0); 
      this.mBoundChaseCamera.setBounds(0, tmxLayer.getWidth(), 0, tmxLayer.getHeight()); 
      this.mBoundChaseCamera.setBoundsEnabled(true); 


      /* Calculate the coordinates for the player, so it's centered on the camera. */ 
      final int centerX = (CAMERA_WIDTH - this.mPlayerTextureRegion.getTileWidth())/2; 
      final int centerY = (CAMERA_HEIGHT - this.mPlayerTextureRegion.getTileHeight())/2; 

      /* Create the sprite and add it to the scene. */ 
      final AnimatedSprite player = new AnimatedSprite(centerX, centerY, this.mPlayerTextureRegion); 
      this.mBoundChaseCamera.setChaseEntity(player); 
      final PhysicsHandler physicsHandler = new PhysicsHandler(player); 
      player.registerUpdateHandler(physicsHandler); 
      scene.attachChild(player); 

      this.mDigitalOnScreenControl = new DigitalOnScreenControl(0, CAMERA_HEIGHT - this.mOnScreenControlBaseTextureRegion.getHeight(), this.mBoundChaseCamera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, new IOnScreenControlListener() { 
        @Override 
        public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) { 
          if (pValueY == 1){ 
            // Up 
            if (playerDirection != PlayerDirection.UP){ 
              player.animate(new long[]{200, 200, 200}, 0, 2, true); 
              playerDirection = PlayerDirection.UP; 
            } 
          }else if (pValueY == -1){ 
            // Down 
            if (playerDirection != PlayerDirection.DOWN){ 
              player.animate(new long[]{200, 200, 200}, 9, 11, true); 
              playerDirection = PlayerDirection.DOWN; 
            } 
          }else if (pValueX == -1){ 
            // Left 
            if (playerDirection != PlayerDirection.LEFT){ 
              player.animate(new long[]{200, 200, 200}, 3, 5, true); 
              playerDirection = PlayerDirection.LEFT; 
            } 
          }else if (pValueX == 1){ 
            // Right 
            if (playerDirection != PlayerDirection.RIGHT){ 
              player.animate(new long[]{200, 200, 200}, 6, 8, true); 
              playerDirection = PlayerDirection.RIGHT; 
            } 
          }else{ 
            if (player.isAnimationRunning()){ 
              player.stopAnimation(); 
              playerDirection = PlayerDirection.NONE; 
            } 
          } 
          physicsHandler.setVelocity(pValueX * 60, pValueY * 60); 
        } 
      }); 
      this.mDigitalOnScreenControl.getControlBase().setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); 
      this.mDigitalOnScreenControl.getControlBase().setAlpha(0.5f); 
      this.mDigitalOnScreenControl.getControlBase().setScaleCenter(0, 128); 
      this.mDigitalOnScreenControl.getControlBase().setScale(1.25f); 
      this.mDigitalOnScreenControl.getControlKnob().setScale(1.25f); 
      this.mDigitalOnScreenControl.refreshControlKnobPosition(); 

      scene.setChildScene(this.mDigitalOnScreenControl); 

      return scene; 

    } 

    @Override 
    public void onLoadComplete() { 
      // TODO Auto-generated method stub 

    } 

}

+1

此溶液DigitalOnScreenControl。對於AnalogOnScreenControl,你可以找到角度'MathUtils.radToDeg((float)Math.atan2(pValueX,pValueY))' – Alexey

+0

@謝謝:謝謝你...... –