SyntaxHighlighter

2012年1月7日土曜日

【AndEngine】TMX map を使った簡単な例 スプライトとDigitalOnScreenControl

前回の続き


今回の内容
・TMX mapにプレイヤーのスプライトを表示
・プレイヤーをDigitalOnScreenControlで操作できるようにする。


概要
onLoadResources()
・スプライトのテクスチャ読み込み
・DigitalOnScreenControlのテクスチャを読み込み
(画像については前回同様、TileCollision.zipに含まれている画像を使用)

onLoadScene()
・プレイヤーを表示
・DigitalOnScreenControlでプレイヤーを操作

クラス変数にenumを定義。その他必要な物は元のソースを参考に追加

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


onLoadResources()

public void onLoadResources() {
  BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/"); // assets/gfx配下に画像が置かれていること
  // Control texture
  this.mOnScreenControlTexture = new BitmapTextureAtlas(256, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
  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);

  // Player sprite texture
  this.mTexturePlayer = new BitmapTextureAtlas(128, 128, TextureOptions.DEFAULT);
  this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mTexturePlayer, this, "hero.png", 0, 0, 3, 4);

  // Load the textures
  this.mEngine.getTextureManager().loadTextures(this.mTexturePlayer, this.mOnScreenControlTexture);
}



onLoadScene()

public Scene onLoadScene() {

  /*・・省略(TMX mapの読み込み等)・・*/
 
  //プレイヤーのアニメーションをインスタンス化
  final int centerX = (CAMERA_WIDTH - this.mPlayerTextureRegion.getTileWidth()) / 2;
  final int centerY = (CAMERA_HEIGHT - this.mPlayerTextureRegion.getTileHeight()) / 2;
  final AnimatedSprite player = new AnimatedSprite(centerX, centerY, this.mPlayerTextureRegion);
 
  //Sceneにプレイヤーを結びつける
  mScene.attachChild(player);
 
  //プレイヤーのコントロール
  this.mDigitalOnScreenControl = new DigitalOnScreenControl(0,
      CAMERA_HEIGHT - this.mOnScreenControlBaseTextureRegion.getHeight(),
      this.mBoundChaseCamera,
      this.mOnScreenControlBaseTextureRegion,
      this.mOnScreenControlKnobTextureRegion, 0.1f,
      new IOnScreenControlListener() {
        public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
          // Set the correct walking animation
          if (pValueY == 1){
            // Up
            if (playerDirection != PlayerDirection.UP){
              player.animate(ANIMATE_DURATION, 0, 2, true);
              playerDirection = PlayerDirection.UP;
            }
          }else if (pValueY == -1){
            // Down
            if (playerDirection != PlayerDirection.DOWN){
              player.animate(ANIMATE_DURATION, 9, 11, true);
              playerDirection = PlayerDirection.DOWN;
            }
          }else if (pValueX == -1){
            // Left
            if (playerDirection != PlayerDirection.LEFT){
              player.animate(ANIMATE_DURATION, 3, 5, true);
              playerDirection = PlayerDirection.LEFT;
            }
          }else if (pValueX == 1){
            // Right
            if (playerDirection != PlayerDirection.RIGHT){
              player.animate(ANIMATE_DURATION, 6, 8, true);
              playerDirection = PlayerDirection.RIGHT;
            }
          }else{
            if (player.isAnimationRunning()){
              player.stopAnimation();
              playerDirection = PlayerDirection.NONE;
            }
          }
        }
    });
  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.getControlKnob().setAlpha(0.5f);
  this.mDigitalOnScreenControl.refreshControlKnobPosition();
  //Sceneの
  mScene.setChildScene(this.mDigitalOnScreenControl);

  return mScene;
}


実行するとこうなります。



今回の内容ではプレイヤーの向きが変わるだけです。
次回ではプレイヤーが移動するようにします。

0 件のコメント:

コメントを投稿