AndEngine游戏编程学习(一)

来源:互联网 发布:问道手游优化 编辑:程序博客网 时间:2024/05/16 10:07

    最近开始想自己做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命令下载,注意下图右上角。



要运行起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;@Overridepublic 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);}@Overrideprotected void onCreateResources() {// TODO Auto-generated method stub}@Overrideprotected 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;}}




原创粉丝点击