AndEngine的第一个程序HelloWorld

AndEngine的环境搭建好后,只运行Samples是远远不够的,要想真正了解如何使用这个框架,还是需要自己动手写写代码。

先从简单的HelloWorld开始。

目标:使用一张图片作为背景,并在屏幕中央显示“Hello World !”文字。

1.新建一个Android工程,设置工程属性,让工程引用AndEngine工程。

2.让工程的主Activity继承SimpleBaseGameActivity类。并实现基类中的几个方法:

public EngineOptions onCreateEngineOptions();
public void onCreateResources();
public Scene onCreateScene();
这个类的全部代码如下:

package com.example.testandeng1;

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.Entity;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;

import android.graphics.Color;
import android.opengl.GLES20;

public class MainActivity extends SimpleBaseGameActivity {
	public static final int CAMERA_WIDTH = 800;
	public static final int CAMERA_HEIGHT = 480;

	private static final int LAYER_COUNT = 2;

	private static final int LAYER_BACKGROUND = 0;
	private static final int LAYER_TEXT = LAYER_BACKGROUND + 1;

	private Scene mScene;

	private Font mFont;
	private BitmapTextureAtlas mBackgroundTexture;
	private ITextureRegion mBackgroundTextureRegion;
	
	private Text mHelloText;

	public EngineOptions onCreateEngineOptions() {
		Camera mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		final EngineOptions engineOptions = new EngineOptions(true,
				ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(
						CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);
		return engineOptions;
	}

	@Override
	public void onCreateResources(){
		/* Load the font we are going to use. */
		FontFactory.setAssetBasePath("font/");
		this.mFont = FontFactory.createFromAsset(this.getFontManager(),
				this.getTextureManager(), 512, 512, TextureOptions.BILINEAR,
				this.getAssets(), "GOTHIC.TTF", 32, true, Color.WHITE);
		this.mFont.load();

		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("img/");
		/* Load all the textures this game needs. */
		this.mBackgroundTexture = new BitmapTextureAtlas(
				this.getTextureManager(), 800, 480);
		this.mBackgroundTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(this.mBackgroundTexture, this, "android.png",
						0, 0);
		this.mBackgroundTexture.load();
	}

	@Override
	public Scene onCreateScene(){
		this.mScene = new Scene();
		for (int i = 0; i < LAYER_COUNT; i++) {
			this.mScene.attachChild(new Entity());
		}

		/* No background color needed as we have a fullscreen background sprite. */
		this.mScene.setBackgroundEnabled(false);
		this.mScene.getChildByIndex(LAYER_BACKGROUND).attachChild(
				new Sprite(0, 0, this.mBackgroundTextureRegion, this
						.getVertexBufferObjectManager()));
		
		this.mHelloText=new Text(CAMERA_WIDTH/2 - 80, CAMERA_HEIGHT/2 - 16, this.mFont, "Hello World !", this.getVertexBufferObjectManager());
		this.mHelloText.setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
		this.mHelloText.setAlpha(0.6f);
		this.mScene.getChildByIndex(LAYER_TEXT).attachChild(this.mHelloText);
		
		return this.mScene;
	}
	
}
这个示例的源码下载地址:

http://download.csdn.net/detail/weyson/4512468



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值