[记录]AndEngine 记录二:框架初始使用

AndEngine提供了一个BaseGameActivity,查看源码得知,BaseGameActivity继承了Android的Activity并实现了IGameInterface, IRendererListener接口。

使用AndEngine第一步就是继承BaseGameActivity,并实现以下四个方法:

onCreateEngineOptions()

onCreateResources(OnCreateResourcesCallback)

onCreateScene(OnCreateSceneCallback)

onPopulateScene(Scene, OnPopulateSceneCallback)

下面贴出完整源码:

public class MainActivity extends BaseGameActivity {
private static final String TAG="con.xidige.testandroid.MainActivity";
private static int CAMERA_WIDTH=800;
private static int CAMERA_HEIGHT=480;
private Camera mCamera;
private Scene scene;
private Random random;
private VertexBufferObjectManager vertexBufferObjectManager;
@Override
public EngineOptions onCreateEngineOptions() {
// TODO Auto-generated method stub
Log.d(TAG, "onCreateEngineOptions");
mCamera=new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, 
new RatioResolutionPolicy(CAMERA_WIDTH/2, CAMERA_HEIGHT), mCamera);
}
@Override
public void onCreateResources(
OnCreateResourcesCallback pOnCreateResourcesCallback)
throws Exception {
Log.d(TAG, "onCreateResources");
// TODO Auto-generated method stub
scene=new Scene();
random=new Random();
vertexBufferObjectManager=getVertexBufferObjectManager();
pOnCreateResourcesCallback.onCreateResourcesFinished();//这个在内部调用了onCreateScene
}
@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
throws Exception {
Log.d(TAG, "onCreateScene");
// TODO Auto-generated method stub
final FPSLogger fpsLogger=new FPSLogger();
mEngine.registerUpdateHandler(fpsLogger);
scene.clearChildScene();
scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));
for (int i = 0; i < 10; 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);

//随机画一些方形
Rectangle rectangle=new Rectangle(x1, y1, 100, 100, vertexBufferObjectManager);
rectangle.setColor(random.nextFloat(), random.nextFloat(), random.nextFloat());
scene.attachChild(rectangle);

}
pOnCreateSceneCallback.onCreateSceneFinished(scene);//这里内部是把把scene传给了engine实例,并调用了onPopulateScene
}
@Override
public void onPopulateScene(Scene pScene,
OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
Log.d(TAG, "onPopulateScene");
// TODO Auto-generated method stub
pOnPopulateSceneCallback.onPopulateSceneFinished();//后台线程,注释掉的话,fpsLogger将不会被启动
}
}
注:注意后面三个方法最后都调用了回调函数,各回调函数内部都调用了下一步方法,最后就形成了这样的调用顺序:onCreateEngineOptions-->onCreateResources-->onCreateScene-->onPopulateScene
» 本文链接地址:https://www.xidige.com/381

打开支付宝扫一扫,即可进行扫码打赏哦

扫码支持
扫码打赏,你说多少就多少

标签:

分享到:

扫一扫 在手机阅读、分享本文

上一篇: 下一篇:
评论区0人评论123人参与

电子邮件地址不会被公开。 必填项已用*标注

*

loading

赞助商广告