Android使用AndEngine创建第一个程序

首先要把andengine.jar复制到libs文件夹里

package com.hu.anden;

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.sprite.Sprite;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

public class MainActivity extends BaseGameActivity {

	public static int CAMERA_WIDTH = 320;
	public static int CAMERA_HEIGHT = 480;
	
	public Camera mCamera;
	public Scene mScene;
	private BitmapTextureAtlas bgTexture;
	private TextureRegion background;
	
	public Engine onLoadEngine() {
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);//创建相机
		return new Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT,
				new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),
				this.mCamera));
	}

	public void onLoadResources() {
		bgTexture = new BitmapTextureAtlas(1024, 1024,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		background = BitmapTextureAtlasTextureRegionFactory.createFromAsset(//从资产读取图片
				bgTexture, this, "colorful.png", 0, 0);
		this.getEngine().getTextureManager().loadTextures(bgTexture);
	}

	public Scene onLoadScene() {
		mScene = new Scene();
		mScene.setTouchAreaBindingEnabled(true);
		return mScene;
	}

	public void onLoadComplete() {
		Sprite bgd = new Sprite(0, 0, background);
		mScene.attachChild(bgd);
	}
    
}

Android使用AndEngine创建第一个程序

 

你可能感兴趣的:(Android使用AndEngine创建第一个程序)