上一篇已经对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();
}
}