最近開始想自己做Android遊戲,大半年沒接觸過程式設計了,以前接觸過libgdx和AndEngine引擎做過點小東西,感覺AndEngine封裝的更加完善,用起來跟容易,就決定采用它來做,加快遊戲開發進度,很久不見發現AndEngine的作者更新很勤快,代碼托管也換了地方,好多東西變了,要重新拾起要花點心思,于是決定開個部落格來記錄我的學習曆程。
既然決定要用AndEngine引擎開發 首先要把引擎源碼和執行個體下載下傳下來。
(一) 下載下傳AndEngine庫和AndEngineExample
以前AndEngine托管在 http://code.google.com/p/andengine/ 上,進去一看發現作者換托管平台了 ,現在源碼托管在 http://github.com/nicolasgramlich/AndEngine
上,下載下傳時候有發現了點問題,不能直接下載下傳。于是百度 google弄了半天,發現要用到github工具下載下傳,于是下載下傳安裝之,和以前用的SVN有類似的地方,選好一個檔案夾右鍵打開程式,發現是一個輸指令行的界面。下載下傳時還要注意的是首先要在網頁上先Fork想要下載下傳的檔案才能使用git clone指令下載下傳,注意下圖右上角。
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZwpmLxETMx81M1ETOxEDN2MTMvwFNy8CXzAzMxAjMvw1ckF2bsBXdvwFdl5mLuR2cj5Set1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
要運作起ANdEngine的示例要先後插起10個檔案,包括源碼 ,示例程式和可選的庫檔案。慢慢下吧。
(二) 運作 AndEngine 示例 把下載下傳的10個檔案統統導入workspace ,基本就可以把示例跑起來了。如果沒跑起來或報錯,一般是版本問題 要確定你的開發工具是ADT - 17或更高。
好了,先慢慢看示例和代碼吧,通常情況模拟器跑不起來,你得建立模拟器時要選擇 US HOST GPU 。因為最新的AndEngine用的是 GLES2.0,模拟器不支援,但選了這個選項的模拟器可以使用。
(三)按照慣例,寫個HelloWorld
package com.zm.game.helloworld;
import java.util.Random;
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.primitive.Line;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.util.FPSLogger;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.ui.activity.SimpleBaseGameActivity;
public class DrawLine extends SimpleBaseGameActivity {
private int CAMERA_WIDTH = 480;
private int CAMERA_HEIGHT = 800;
@Override
public EngineOptions onCreateEngineOptions() {
final Camera mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED,
new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);
}
@Override
protected void onCreateResources() {
// TODO Auto-generated method stub
}
@Override
protected Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene();
scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));
final VertexBufferObjectManager vertexBufferObjectManager = this
.getVertexBufferObjectManager();
Random random = new Random();
for (int i = 0; i < 100; i++) {
final float x1 = random.nextFloat() * CAMERA_WIDTH;
final float x2 = random.nextFloat() * CAMERA_WIDTH;
final float y1 = random.nextFloat() * CAMERA_HEIGHT;
final float y2 = random.nextFloat() * CAMERA_HEIGHT;
final float lineWidth = random.nextFloat() * 5;
final Line line = new Line(x1, y1, x2, y2, lineWidth,
vertexBufferObjectManager);
line.setColor(random.nextFloat(), random.nextFloat(),
random.nextFloat());
scene.attachChild(line);
}
return scene;
}
}