AndEngine中手柄的使用——DigitalOnScreenControl && AnalogOnScreenControl

來源:互聯網
上載者:User

package org.andengine.examples;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.camera.hud.controls.AnalogOnScreenControl;
import org.andengine.engine.camera.hud.controls.AnalogOnScreenControl.IAnalogOnScreenControlListener;
import org.andengine.engine.camera.hud.controls.BaseOnScreenControl;
import org.andengine.engine.handler.physics.PhysicsHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.input.touch.controller.MultiTouch;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.math.MathUtils;

import android.opengl.GLES20;
import android.widget.Toast;

/**
 * (c) 2010 Nicolas Gramlich
 * (c) 2011 Zynga
 *
 * @author Nicolas Gramlich
 * @since 00:06:23 - 11.07.2010
 */
public class AnalogOnScreenControlsExample extends SimpleBaseGameActivity {
 // ===========================================================
 // Constants
 // ===========================================================

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

 // ===========================================================
 // Fields
 // ===========================================================

 private Camera mCamera;

 private BitmapTextureAtlas mBitmapTextureAtlas;
 private ITextureRegion mFaceTextureRegion;

 private BitmapTextureAtlas mOnScreenControlTexture;
 private ITextureRegion mOnScreenControlBaseTextureRegion;
 private ITextureRegion mOnScreenControlKnobTextureRegion;

 private boolean mPlaceOnScreenControlsAtDifferentVerticalLocations = false;

 // ===========================================================
 // Constructors
 // ===========================================================

 // ===========================================================
 // Getter & Setter
 // ===========================================================

 // ===========================================================
 // Methods for/from SuperClass/Interfaces
 // ===========================================================

 @Override
 public EngineOptions onCreateEngineOptions() {
  this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

  final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
  engineOptions.getTouchOptions().setNeedsMultiTouch(true);
//設定多點觸控,因為有兩個手柄
  if(MultiTouch.isSupported(this)) {
   if(MultiTouch.isSupportedDistinct(this)) {
    Toast.makeText(this, "MultiTouch detected --> Both controls will work properly!", Toast.LENGTH_SHORT).show();
   } else {
    this.mPlaceOnScreenControlsAtDifferentVerticalLocations = true;
    Toast.makeText(this, "MultiTouch detected, but your device has problems distinguishing between fingers.\n\nControls are placed at different vertical locations.", Toast.LENGTH_LONG).show();
   }
  } else {
   Toast.makeText(this, "Sorry your device does NOT support MultiTouch!\n\n(Falling back to SingleTouch.)\n\nControls are placed at different vertical locations.", Toast.LENGTH_LONG).show();
  }

  return engineOptions;
 }

 @Override
 public void onCreateResources() {
  BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

  this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.BILINEAR);
  this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "face_box.png", 0, 0);
  this.mBitmapTextureAtlas.load();

  this.mOnScreenControlTexture = new BitmapTextureAtlas(this.getTextureManager(), 256, 128, TextureOptions.BILINEAR);
  this.mOnScreenControlBaseTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_base.png", 0, 0);
  this.mOnScreenControlKnobTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_knob.png", 128, 0);
  this.mOnScreenControlTexture.load();
 }

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

  final Scene scene = new Scene();
  scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));

  final float centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
  final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;
  final Sprite face = new Sprite(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
  final PhysicsHandler physicsHandler = new PhysicsHandler(face);
  face.registerUpdateHandler(physicsHandler);

  scene.attachChild(face);

//這是第一個手柄,控制平移的

  /* Velocity control (left). */
  final float x1 = 0;
  final float y1 = CAMERA_HEIGHT - this.mOnScreenControlBaseTextureRegion.getHeight();
  final AnalogOnScreenControl velocityOnScreenControl = new AnalogOnScreenControl(x1, y1, this.mCamera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, 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) {
    /* Nothing. */
   }
  });
  
  velocityOnScreenControl.getControlBase().setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
  velocityOnScreenControl.getControlBase().setAlpha(0.5f);

  scene.setChildScene(velocityOnScreenControl);

//這是第二個手柄,控制旋轉的
  /* Rotation control (right). */
  final float y2 = (this.mPlaceOnScreenControlsAtDifferentVerticalLocations) ? 0 : y1;
  final float x2 = CAMERA_WIDTH - this.mOnScreenControlBaseTextureRegion.getWidth();
  final AnalogOnScreenControl rotationOnScreenControl = new AnalogOnScreenControl(x2, y2, this.mCamera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, this.getVertexBufferObjectManager(), new IAnalogOnScreenControlListener()
{
   @Override
   public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
    if(pValueX == x1 && pValueY == x1) {
     face.setRotation(x1);
    } else {
     face.setRotation(MathUtils.radToDeg((float)Math.atan2(pValueX, -pValueY)));
    }
   }

   @Override
   public void onControlClick(final AnalogOnScreenControl pAnalogOnScreenControl) {
    /* Nothing. */
   }
  });

//設定混合色的代碼
  rotationOnScreenControl.getControlBase().setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
  rotationOnScreenControl.getControlBase().setAlpha(0.5f);

  velocityOnScreenControl.setChildScene(rotationOnScreenControl);

  return scene;
 }

 // ===========================================================
 // Methods
 // ===========================================================

 // ===========================================================
 // Inner and Anonymous Classes
 // ===========================================================
}

在視野(scene)中有三個對象,分別為被控制的對象(face),左邊的手柄(velocityOnScreenControl),右邊的手柄,(rotationOnScreenControl),將這三個對象分別加入到視野中用到如下三句核心代碼:scene.attachChild(face); scene.setChildScene(velocityOnScreenControl);

velocityOnScreenControl.setChildScene(rotationOnScreenControl);這時注意第三句用的是velocityOnScreenControl而並不是scene,原因是:如果用的是scene的話,就會覆蓋前一個手柄。

以上用的是AnalogOnScreenControl(類比手柄),當然了,我們也可以使用另一種”數字手柄“,即DigitalOnScreenControl,下面直接看代碼啦:

package org.andengine.examples;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.camera.hud.controls.BaseOnScreenControl;
import org.andengine.engine.camera.hud.controls.BaseOnScreenControl.IOnScreenControlListener;
import org.andengine.engine.camera.hud.controls.DigitalOnScreenControl;
import org.andengine.engine.handler.physics.PhysicsHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.opengl.GLES20;

/**
 * (c) 2010 Nicolas Gramlich
 * (c) 2011 Zynga
 *
 * @author Nicolas Gramlich
 * @since 00:06:23 - 11.07.2010
 */
public class DigitalOnScreenControlExample extends SimpleBaseGameActivity {
 // ===========================================================
 // Constants
 // ===========================================================

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

 // ===========================================================
 // Fields
 // ===========================================================

 private Camera mCamera;

 private BitmapTextureAtlas mBitmapTextureAtlas;
 private ITextureRegion mFaceTextureRegion;

 private BitmapTextureAtlas mOnScreenControlTexture;
 private ITextureRegion mOnScreenControlBaseTextureRegion;
 private ITextureRegion mOnScreenControlKnobTextureRegion;

 private DigitalOnScreenControl mDigitalOnScreenControl;

 // ===========================================================
 // Constructors
 // ===========================================================

 // ===========================================================
 // Getter & Setter
 // ===========================================================

 // ===========================================================
 // Methods for/from SuperClass/Interfaces
 // ===========================================================

 @Override
 public EngineOptions onCreateEngineOptions() {
  this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

  return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
 }

 @Override
 public void onCreateResources() {
  BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

  this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.BILINEAR);
  this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "face_box.png", 0, 0);
  this.mBitmapTextureAtlas.load();

  this.mOnScreenControlTexture = new BitmapTextureAtlas(this.getTextureManager(), 256, 128, TextureOptions.BILINEAR);
  this.mOnScreenControlBaseTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_base.png", 0, 0);
  this.mOnScreenControlKnobTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_knob.png", 128, 0);
  this.mOnScreenControlTexture.load();
 }

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

  final Scene scene = new Scene();
  scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));

  final float centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
  final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;
  final Sprite face = new Sprite(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
  final PhysicsHandler physicsHandler = new PhysicsHandler(face);
  face.registerUpdateHandler(physicsHandler);

  scene.attachChild(face);

  this.mDigitalOnScreenControl = new DigitalOnScreenControl(0, CAMERA_HEIGHT - this.mOnScreenControlBaseTextureRegion.getHeight(), this.mCamera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, this.getVertexBufferObjectManager(),
new IOnScreenControlListener() {
   @Override
   public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
    physicsHandler.setVelocity(pValueX * 100, pValueY * 100);
   }
  });
  this.mDigitalOnScreenControl.getControlBase().setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.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 onGameCreated() {
  this.showDialog(DIALOG_ALLOWDIAGONAL_ID);
 }

 @Override
 protected Dialog onCreateDialog(final int pID) {
  switch(pID) {
   case DIALOG_ALLOWDIAGONAL_ID:
    return new AlertDialog.Builder(this)
    .setTitle("Setup...")
    .setMessage("Do you wish to allow diagonal directions on the OnScreenControl?")
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(final DialogInterface pDialog, final int pWhich) {
      DigitalOnScreenControlExample.this.mDigitalOnScreenControl.setAllowDiagonal(true);
     }
    })
    .setNegativeButton("No", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(final DialogInterface pDialog, final int pWhich) {
      DigitalOnScreenControlExample.this.mDigitalOnScreenControl.setAllowDiagonal(false);
     }
    })
    .create();
  }
  return super.onCreateDialog(pID);
 }

 // ===========================================================
 // Methods
 // ===========================================================

 // ===========================================================
 // Inner and Anonymous Classes
 // ===========================================================
}

 

該功能是使用了一個手柄來控制物體的移動。DigitalOnScreenControl ,AnalogOnScreenControl的功能類似。

DigitalOnScreenControlDigitalOnScreenControlDigitalOnScreenControlDigitalOnScreenControlDigitalOnScreenControlDigitalOnScreenControl

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.