现在的位置: 首页 > 综合 > 正文

开始学习andEngine(2)

2013年12月04日 ⁄ 综合 ⁄ 共 1919字 ⁄ 字号 评论关闭

最简单的例子,显示背景。

修改主程序

package com.bupt.engine;

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.scene.background.ColorBackground;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.util.Log;

public class EngineTestActivity extends BaseGameActivity {
	public static final String TAG = "fuck";
	private static final int CAMERA_WIDTH = 720;
	private static final int CAMERA_HEIGHT = 480;
	
	private Camera camera;
	@Override
	public Engine onLoadEngine() {
		Log.e(TAG,"onLoadEngine" );
		this.camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, 
				new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.camera));
	}

	@Override
	public void onLoadResources() {
		Log.e(TAG,"onLoadResources" );
		// TODO Auto-generated method stub
		
	}

	@Override
	public Scene onLoadScene() {
		Log.e(TAG,"onLoadScene" );
		this.mEngine.registerUpdateHandler(new FPSLogger());
		final Scene scene = new Scene();
		scene.setBackground(new ColorBackground(0, 0, 8784f));
		return scene;
	}

	@Override
	public void onLoadComplete() {
		Log.e(TAG,"onLoadComplete" );
		// TODO Auto-generated method stub
		
	}
}

另外还需要在AndroidManifest.xml中加入 <uses-permission android:name="android.permission.WAKE_LOCK" />该权限允许我们去锁定一个所谓的wake
lock,使用wake lock我们可以避免在进行游戏的时候设备休眠,如重力感应类游戏,
 Allows using PowerManager WakeLocks to keep processor from sleeping or screen from dimming。

运行后If it builds properly, it should run on your Phone/Emulator and display a bluey background.
If so, well done you have written your first AndEngine
Project! Its as simple as that. 

顺便看一下logcat里的内容:


图一
onLoadEngine->onLoadResources->onLoadScene->onLoadComplete 熟悉吧,(1)里写过。
ps:提醒一句,camera是AndEngine重写的,导包的时候要看准!!

抱歉!评论已关闭.