Andengine矩形的繪製和座標點的關係

來源:互聯網
上載者:User

package cn.zp;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.Entity;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.util.FPSLogger;
import org.andengine.entity.util.ScreenCapture;
import org.andengine.entity.util.ScreenCapture.IScreenCaptureCallback;
import org.andengine.input.touch.TouchEvent;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.FileUtils;

import android.widget.Toast;

/**
 * (c) 2010 Nicolas Gramlich (c) 2011 Zynga Inc.
 *
 * @author Nicolas Gramlich
 * @since 11:54:51 - 03.04.2010
 */
public class MainActivity extends SimpleBaseGameActivity {
 // ===========================================================
 // Constants
 // ===========================================================

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

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

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

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

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

 @Override
 public EngineOptions onCreateEngineOptions() {
  final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

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

 @Override
 public void onCreateResources() {

 }

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

  final Scene scene = new Scene();
  final ScreenCapture screenCapture = new ScreenCapture();
  scene.attachChild(screenCapture);
  scene.setOnSceneTouchListener(new IOnSceneTouchListener() {
   @Override
   public boolean onSceneTouchEvent(final Scene pScene,
     final TouchEvent pSceneTouchEvent) {
    if (pSceneTouchEvent.isActionDown()) {
     screenCapture.capture(180, 60, 360, 360, FileUtils
       .getAbsolutePathOnExternalStorage(
         MainActivity.this,
         "Screen_" + System.currentTimeMillis()
           + ".png"),
       new IScreenCaptureCallback() {
        @Override
        public void onScreenCaptured(
          final String pFilePath) {
         MainActivity.this
           .runOnUiThread(new Runnable() {
            @Override
            public void run() {
             Toast.makeText(
               MainActivity.this,
               "Screenshot: "
                 + pFilePath
                 + " taken!",
               Toast.LENGTH_SHORT)
               .show();
            }
           });
        }

        @Override
        public void onScreenCaptureFailed(
          final String pFilePath,
          final Exception pException) {
         MainActivity.this
           .runOnUiThread(new Runnable() {
            @Override
            public void run() {
             Toast.makeText(
               MainActivity.this,
               "FAILED capturing Screenshot: "
                 + pFilePath
                 + " !",
               Toast.LENGTH_SHORT)
               .show();
            }
           });
        }
       });
    }
    return true;
   }
  });

  scene.setBackground(new Background(0, 0, 0));

  /* Create the rectangles. */
  final Rectangle rect1 = this.makeColoredRectangle(-180, -180, 1, 0, 0);
  final Rectangle rect2 = this.makeColoredRectangle(0, -180, 0, 1, 0);
  final Rectangle rect3 = this.makeColoredRectangle(0, 0, 0, 0, 1);
  final Rectangle rect4 = this.makeColoredRectangle(-180, 0, 1, 1, 0);

  final Entity rectangleGroup = new Entity(CAMERA_WIDTH / 2,
    CAMERA_HEIGHT / 2);

  rectangleGroup.attachChild(rect1);
  rectangleGroup.attachChild(rect2);
  rectangleGroup.attachChild(rect3);
  rectangleGroup.attachChild(rect4);

  scene.attachChild(rectangleGroup);

  return scene;
 }

 private Rectangle makeColoredRectangle(final float pX, final float pY,
   final float pRed, final float pGreen, final float pBlue) {
  final Rectangle coloredRect = new Rectangle(pX, pY, 180, 180,
    this.getVertexBufferObjectManager());
  coloredRect.setColor(pRed, pGreen, pBlue);
  return coloredRect;
 }

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

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

小結:正確理解螢幕座標有利於我們更好的繪製圖形,以及所繪製圖形的相對關係

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.