Android AndEngine引擎运用

package V5.co.cc;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.text.ChangeableText;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.graphics.Color;
import android.graphics.Typeface;

public class V5Activity extends
  BaseGameActivity {
 private static final int CAMERA_WIDTH = 320;
 private static final int CAMERA_HEIGHT = 480;
 private Camera andCamera;
 private Texture myFontTexture;
 private Font myFont;

 @Override
 public Engine onLoadEngine() {
  // TODO Auto-generated method stub
  this.andCamera = new Camera(0,
    0, CAMERA_WIDTH,
    CAMERA_HEIGHT);
  // 构建Engine,全屏显示,手机方向为竖屏,按比例拉伸
  return new Engine(
    new EngineOptions(
      true,
      ScreenOrientation.PORTRAIT,
      new RatioResolutionPolicy(
        CAMERA_WIDTH,
        CAMERA_HEIGHT),
      this.andCamera));

 }

 @Override
 public void onLoadResources() {
  // TODO Auto-generated method stub
  this.myFontTexture = new Texture(
    256, 256,
    TextureOptions.DEFAULT);
  // 构建字体
  this.myFont = new Font(
    this.myFontTexture,
    Typeface.create(
      Typeface.DEFAULT,
      Typeface.BOLD),
    32, true, Color.WHITE);
  // 注入相关纹理及字体
  this.mEngine
    .getTextureManager()
    .loadTexture(
      this.myFontTexture);
  this.mEngine.getFontManager()
    .loadFont(this.myFont);

 }

 @Override
 public Scene onLoadScene() {
  // TODO Auto-generated method stub
  // 构建场景,允许的最大Layer数量为1
  final Scene scene = new Scene(1);
  // 使用可以变更内容的ChangeableText显示FPS(它的父类Text不允许改变显示内容),位置在15,5,
  // 字体为myFont中所规定的,最多允许显示5个字符(设置能显示几个字符,实际就能显示几个,
  // AndEngine不能自动扩充,不填以初始化时输入的字符数计算……)
  final ChangeableText text = new ChangeableText(
    5, 5, this.myFont,
    "0.0", 5);
  // 注册FPS监听
  this.mEngine
    .registerUpdateHandler(new FPSLogger() {
     protected void onHandleAverageDurationElapsed(
       final float pFPS) {
      super.onHandleAverageDurationElapsed(pFPS);
      // 传递内容到ChangeableText
      text.setText(""
        + pFPS);
     }
    });
  scene.attachChild(text);
  // 构建场景,可容纳图层数为1
  return scene;

 }

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

 }
}

 

------------------test2 AsyncTask异步加载

package V5.co.cc;

import java.util.concurrent.Callable;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.Callback;

import android.util.Log;

public class V5Activity extends
  BaseGameActivity {

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

 /** Called when the activity is first created. */

 @Override
 public Engine onLoadEngine() {
  // TODO Auto-generated method stub
  this.andCamera = new Camera(0,
    0, CAMERA_WIDTH,
    CAMERA_HEIGHT);
  // 构建Engine,全屏显示,手机方向为竖屏,按比例拉伸
  return new Engine(
    new EngineOptions(
      true,
      ScreenOrientation.PORTRAIT,
      new RatioResolutionPolicy(
        CAMERA_WIDTH,
        CAMERA_HEIGHT),
      this.andCamera));

 }

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

  this.doAsync(
    R.string.test1,
    R.string.test2,
    new Callable<Void>() {
     // 希望AndEngine异步加载的数据
     public Void call()
       throws Exception {
      for (int i = 0; i < Integer.MAX_VALUE; i++) {
      }
      return null;
     }
     // 当加载完成后回调,可在此进行一些加载完毕的事后处理
    },
    new org.anddev.andengine.util.Callback<Void>() {
     public void onCallback(
       final Void pCallbackValue) {
      Log.d("Callback",
        "over");
     }
    });

 }

 @Override
 public Scene onLoadScene() {
  // TODO Auto-generated method stub
  final Scene scene = new Scene(1);
  return scene;

 }

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

 

 

 

--------------test3

package V5.co.cc;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.modifier.RotationModifier;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.ui.activity.BaseGameActivity;

public class V5Activity extends
  BaseGameActivity {

 // 屏幕分辨率
 private static final int CAMERA_WIDTH = 320;
 private static final int CAMERA_HEIGHT = 480;
 private TextureRegion myTextureRegion;

 private Camera andCamera;

 /** Called when the activity is first created. */

 @Override
 public Engine onLoadEngine() {
  // TODO Auto-generated method stub
  this.andCamera = new Camera(0,
    0, CAMERA_WIDTH,
    CAMERA_HEIGHT);
  // 构建Engine,全屏显示,手机方向为竖屏,按比例拉伸
  return new Engine(
    new EngineOptions(
      true,
      ScreenOrientation.PORTRAIT,
      new RatioResolutionPolicy(
        CAMERA_WIDTH,
        CAMERA_HEIGHT),
      this.andCamera));

 }

 @Override
 public void onLoadResources() {
  // TODO Auto-generated method stub
  
  //文理的分辨率 图片在该分辨率之下
  Texture myTexture = new Texture(
    512, 512,
    TextureOptions.DEFAULT);
  // 加载指定路径纹理到myTextureRegion

  this.myTextureRegion = TextureRegionFactory
    .createFromAsset(
      myTexture,
      this,
      "res/icon.jpg",
      0, 0);
  // 载入纹理到TextureManager
  this.getEngine()
    .getTextureManager()
    .loadTextures(myTexture);

 }

 @Override
 public Scene onLoadScene() {
  // TODO Auto-generated method stub
  getEngine()
    .registerUpdateHandler(
      new FPSLogger());
  final Scene scene = new Scene(1);
  final int centerX = (CAMERA_WIDTH - this.myTextureRegion
    .getWidth()) / 2;
  final int centerY = (CAMERA_HEIGHT - this.myTextureRegion
    .getHeight()) / 2;
  final Sprite sprite = new Sprite(
    centerX, centerY,
    this.myTextureRegion);
  scene.attachChild(sprite);
  SequenceEntityModifier ballAction = new SequenceEntityModifier(
    new RotationModifier(
      3, 0, 360),
    new RotationModifier(
      3, 360, 0));
  sprite.registerEntityModifier(ballAction);
  return scene;

 }

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

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值