标签存档: helloworld

AndEngine的HelloWorld

AndEngine 今天的主角,是一个 Android 平台的开源 2D OpenGL 游戏引擎,采用 GNU Lesser GPL,包含游戏引擎的常用功能,同时内置了Box2D物理引擎,你可以非常方便的在你的程序里面使用物理效果。。下面将要通过一个 HelloWorld 来介绍 Android 的基本用法。

1.准备工作

1)eclipse、Android SDK的安装配置,这个就不用多说,具体自行Google

2)签出 AndEngine 以及 AndEngineExamples 的代码,eclipse的插件 Mercurial Eclipse 就可以轻松签出,它们的版本库地址分别是

https://andengine.googlecode.com/hg/

https://andengineexamples.googlecode.com/hg/

2.在 Eclipse 中新建一个 Android 项目, File->New->Android Project  ,输入相应的内容,注意Build Target选择 1.6 点击 Finish

3 .把 AndEngine 加入到上一步新建的项目中

方法一:加入 AndEngine 的代码到 项目中,把准备工作中签出的 AndEngine 项目中 src目录的 文件 复制到 上一步新建的项目 目录中的 src 里面。这样做的好处是你可以自由的修改 AndEngine 的源代码

方法二:加入 andengine.jar 到项目中,在上一步新建的项目中新建文件夹 “lib”,把准备工作中签出的 AndEngine 项目中 lib 目录下面的 andengine.jar,在 eclipse 的 Package Explorer 中的新建项目中找到  andengine.jar ,右键->Build Path ->Add to Build Path。这样做的好处是方便简洁

4.HelloWorld,修改项目中原有的 Activity,代码如下,注意类名的一致

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.entity.primitive.Line;
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.engine.camera.Camera;
import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;

public class helloae extends BaseGameActivity {
      // ===========================================================
    // Constants
    // ===========================================================

    private static final int CAMERA_WIDTH = 720;
    private static final int CAMERA_HEIGHT = 480;

    // ===========================================================
    // Fields
    // ===========================================================

    private Camera mCamera;

    // ===========================================================
    // Constructors
    // ===========================================================

    // ===========================================================
    // Getter & Setter
    // ===========================================================

    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================

    @Override
    public Engine onLoadEngine() {
            this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
            return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
    }

    @Override
    public void onLoadResources() {
    }

    @Override
    public Scene onLoadScene() {
            this.mEngine.registerUpdateHandler(new FPSLogger());

            final Scene scene = new Scene(1);
            scene.setBackground(new ColorBackground(1.0f, 1.0f, 1.0f));
            final Line line = new Line(0,240, 720, 240, 5.0f);

            line.setColor(1, 0, 0);
            scene.getFirstChild().attachChild(line);
           
            return scene;
    }

    @Override
    public void onLoadComplete() {

    }

    // ===========================================================
    // Methods
    // ===========================================================

    // ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================
}

添加完这段代码之后,别忘了在你项目的AndroidManifest.xml 中添加
<uses-permission android:name=”android.permission.WAKE_LOCK”/ >

5.运行

运行截图

没错,就是在屏幕中间绘制一条红色的横线。
参考:AndEngine初步