andengine编程之Scene

上一篇已经对andengine做了简单介绍,接下来,我们将在介绍scene这个类


首先,你要准备两张图片,一张作为背景图,一张作为button,button的最好是两张背景不同的,用来表现出按下与不按下的效果。

素材准备好后,可以开始代码的编写了,接着上一个TestAndEngine继续写。

在onCreateEngineOptions中,构建EngineOptions的参数设置:

private static final int CAMERA_WIDTH = 800;
	private static final int CAMERA_HEIGHT = 480;
	private static final float CAMERA_MOVE_VELOCITY = 3000;
	private static final float CAMERA_ZOOM_VELOCITY = 5;

	public EngineOptions onCreateEngineOptions() {
		SmoothCamera mCamera = new SmoothCamera(0, 0, CAMERA_WIDTH,
				CAMERA_HEIGHT, CAMERA_MOVE_VELOCITY, CAMERA_MOVE_VELOCITY,
				CAMERA_ZOOM_VELOCITY);

		EngineOptions mEngineOptions = new EngineOptions(true,
				ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(),
				mCamera);

		return mEngineOptions;
	}


然后我们来加载资源:

public void onCreateResources(
			OnCreateResourcesCallback pOnCreateResourcesCallback)
			throws Exception {
		// 1024, 512:这里的图片都要创建成2几次方的形式,例如128、256、512,原始图片也要创建成这样大小
		// TextureOptions.DEFAULT:使用默认的纹理效果,当然你也可选其他的形式
		BitmapTextureAtlas backgroundTextureAtlas = new BitmapTextureAtlas(
				getTextureManager(), 1024, 512, TextureOptions.DEFAULT);
		// 图片要保存在asset文件夹下,如果图片很多,可以采用再建立文件夹加以区分
		// 然后用来设置根目录:
		// BitmapTextureAtlasTextureRegionFactory.setAssetBasePath(final String
		// pAssetBasePath);
		background = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				backgroundTextureAtlas, this, "background.png", 0, 0);

		// 最后一定要调用这个方法,否则图片不会加载上来
		backgroundTextureAtlas.load();

		// 同上
		BitmapTextureAtlas buttonTextureAtlas = new BitmapTextureAtlas(
				getTextureManager(), 256, 256, TextureOptions.DEFAULT);
		// 通过帧序列块的方式创建button,注意顺序:第一张为正常效果,第二张为按下效果
		// 1, 2:共1列,有2行
		button = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(
				buttonTextureAtlas, this, "button.png", 0, 0, 1, 2);
		// 加载一下
		buttonTextureAtlas.load();

		// 最后一定要调用,通知程序可以开始调用onCreateScene方法
		pOnCreateResourcesCallback.onCreateResourcesFinished();
	}



图片加载完后,我们还要构建场景,这个里面涉及到了sprite,我们用sprite来构建背景和按钮,sprite的扩展类中有ButtonSprite,很好的实现了button的操作。

public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
			throws Exception {

		// 构建场景
		final Scene mScene = new Scene();

		// 建立背景Sprite
		Sprite backgroundSprite = new Sprite(0, 0, background,
				getVertexBufferObjectManager());

		// 建立按钮Sprite
		// 480, 200:显示的位置
		// button:为按钮图片帧
		buttonSprite = new ButtonSprite(480, 200, button,
				getVertexBufferObjectManager(), new OnClickListener() {

					// 建立监听,当用户点住不放的时候,button图片会切换,但不会执行onClick里的操作
					// 当用户松开按钮的时候,才会执行
					public void onClick(ButtonSprite pButtonSprite,
							float pTouchAreaLocalX, float pTouchAreaLocalY) {

						// 当用户点下后,我们将这个button从场景中移除掉
						mScene.detachChild(buttonSprite);
					}
				});

		// 将两个Sprite添加进场景
		mScene.attachChild(backgroundSprite);
		mScene.attachChild(buttonSprite);

		// 注册buttonSprite的触摸机制
		mScene.registerTouchArea(buttonSprite);

		// 最后一定要调用,通知系统我们创建了哪个scene
		pOnCreateSceneCallback.onCreateSceneFinished(mScene);
	}


最后,记得,一定要写好:

	public void onPopulateScene(Scene pScene,
			OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
		// 最后千万不要忘记了这个
		pOnPopulateSceneCallback.onPopulateSceneFinished();
	}

否则,线程是不会跑起来刷屏的!


以下是完整的代码:

package com.test;

import org.andengine.engine.camera.SmoothCamera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.ButtonSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.sprite.ButtonSprite.OnClickListener;
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.opengl.texture.region.TiledTextureRegion;
import org.andengine.ui.activity.BaseGameActivity;

public class TestAndEngine extends BaseGameActivity {

	private static final int CAMERA_WIDTH = 800;
	private static final int CAMERA_HEIGHT = 480;
	private static final float CAMERA_MOVE_VELOCITY = 3000;
	private static final float CAMERA_ZOOM_VELOCITY = 5;

	private ITextureRegion background;

	private TiledTextureRegion button;

	private ButtonSprite buttonSprite;

	public EngineOptions onCreateEngineOptions() {
		SmoothCamera mCamera = new SmoothCamera(0, 0, CAMERA_WIDTH,
				CAMERA_HEIGHT, CAMERA_MOVE_VELOCITY, CAMERA_MOVE_VELOCITY,
				CAMERA_ZOOM_VELOCITY);

		EngineOptions mEngineOptions = new EngineOptions(true,
				ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(),
				mCamera);
		mEngineOptions.getAudioOptions().setNeedsMusic(true);
		mEngineOptions.getAudioOptions().setNeedsSound(true);

		return mEngineOptions;
	}

	public void onCreateResources(
			OnCreateResourcesCallback pOnCreateResourcesCallback)
			throws Exception {
		// 1024, 512:这里的图片都要创建成2几次方的形式,例如128、256、512,原始图片也要创建成这样大小
		// TextureOptions.DEFAULT:使用默认的纹理效果,当然你也可选其他的形式
		BitmapTextureAtlas backgroundTextureAtlas = new BitmapTextureAtlas(
				getTextureManager(), 1024, 512, TextureOptions.DEFAULT);
		// 图片要保存在asset文件夹下,如果图片很多,可以采用再建立文件夹加以区分
		// 然后用来设置根目录:
		// BitmapTextureAtlasTextureRegionFactory.setAssetBasePath(final String
		// pAssetBasePath);
		background = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				backgroundTextureAtlas, this, "background.png", 0, 0);

		// 最后一定要调用这个方法,否则图片不会加载上来
		backgroundTextureAtlas.load();

		// 同上
		BitmapTextureAtlas buttonTextureAtlas = new BitmapTextureAtlas(
				getTextureManager(), 256, 256, TextureOptions.DEFAULT);
		// 通过帧序列块的方式创建button,注意顺序:第一张为正常效果,第二张为按下效果
		// 1, 2:共1列,有2行
		button = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(
				buttonTextureAtlas, this, "button.png", 0, 0, 1, 2);
		// 加载一下
		buttonTextureAtlas.load();

		// 最后一定要调用,通知程序可以开始调用onCreateScene方法
		pOnCreateResourcesCallback.onCreateResourcesFinished();
	}

	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
			throws Exception {

		// 构建场景
		final Scene mScene = new Scene();

		// 建立背景Sprite
		Sprite backgroundSprite = new Sprite(0, 0, background,
				getVertexBufferObjectManager());

		// 建立按钮Sprite
		// 480, 200:显示的位置
		// button:为按钮图片帧
		buttonSprite = new ButtonSprite(480, 300, button,
				getVertexBufferObjectManager(), new OnClickListener() {

					// 建立监听,当用户点住不放的时候,button图片会切换,但不会执行onClick里的操作
					// 当用户松开的时候,才会执行
					public void onClick(ButtonSprite pButtonSprite,
							float pTouchAreaLocalX, float pTouchAreaLocalY) {
						// 当用户点下后,我们将这个button从场景中移除掉
						mScene.detachChild(buttonSprite);
					}
				});

		// 将两个Sprite添加进场景
		mScene.attachChild(backgroundSprite);
		mScene.attachChild(buttonSprite);

		// 注册buttonSprite的触摸机制
		mScene.registerTouchArea(buttonSprite);

		// 最后一定要调用,通知系统我们创建了哪个scene
		pOnCreateSceneCallback.onCreateSceneFinished(mScene);
	}

	public void onPopulateScene(Scene pScene,
			OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
		// 最后千万不要忘记了这个
		pOnPopulateSceneCallback.onPopulateSceneFinished();
	}

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值