AndEngine Example(1):LineExample

12 篇文章 0 订阅
9 篇文章 0 订阅

目标:

1.使得程序可以正常运行

2.搭建最简单的场景

3.理解Scene,Camera等几个基本的概念

4.理解整个的基本流程


----------------------------------------------------

新建一个Android工程,自动创建Activity,然后为其命名为LineExample。(如果改名了,不要忘记修改Manifest)

将下面的代码复制进去,

然后添加andengine.jar到libs中。

运行。


package org.andengine.examples;

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;

/**
 * (c) 2010 Nicolas Gramlich 
 * (c) 2011 Zynga Inc.
 * 
 * @author Nicolas Gramlich
 * @since 11:54:51 - 03.04.2010
 */
public class LineExample extends SimpleBaseGameActivity {
	// ===========================================================
	// Constants
	// ===========================================================

	/* Initializing the Random generator produces a comparable result over different versions. */
	private static final long RANDOM_SEED = 1234567890;

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

	private static final int LINE_COUNT = 100;

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

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

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

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

	@Override
	public EngineOptions onCreateEngineOptions() {
		final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

		return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
	}

	@Override
	public void onCreateResources() {

	}

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

		final Scene scene = new Scene();
		scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));

		final Random random = new Random(RANDOM_SEED);

		final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();
		for(int i = 0; i < LINE_COUNT; 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;
	}

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

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



onCreateEngineOptions()将会创建一个EngineOptions 对象

EngineOptions 引擎的参数,创建它的时候,需要给定:屏幕方向,分辨率比率策略,和相机(这个相机是一个抽象的概念,并不是实际的硬件)


onCreateScene() 创建一个场景

这里,我们创建了一个场景,并且往里面加入了实体(Line is a Entity)

           Line extends Shape

           Shape extends Entity 


VertexBufferObjectManager 是一个管理者,它管理这些实体对象的buffer。


--------------------------------------------------------------------------

扩展阅读:绘制是如何进行的?

EngineRenderer implements GLSurfaceView.Renderer

凡是对于OpenGL有一定了解的应该知道

GLSurfaceView.Renderer中有一个回调函数: public void onDrawFrame(final GL10 pGL)

继承类在此回调中调用了

(1) Engine.onDrawFrame

(2) Engine.onUpdateDrawHandlers(pGLState, this.mCamera)

(3) DrawHandlerList.onDraw(final GLState pGLState, final Camera pCamera)

(4) IDrawHandler.onDraw(final GLState pGLState, final Camera pCamera)

(5) interface IEntity extends IDrawHandler

(6) class Entity implements IEntity

(7) Entity.onDraw(final GLState pGLState, final Camera pCamera)

(8) Entity.onManagedDraw(final GLState pGLState, final Camera pCamera) 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值