Andengine游戏引擎的学习之游戏菜单

package cn.flyaudio.game;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.menu.MenuScene;
import org.anddev.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.anddev.andengine.entity.scene.menu.animator.SlideMenuAnimator;
import org.anddev.andengine.entity.scene.menu.item.IMenuItem;
import org.anddev.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.anddev.andengine.entity.scene.menu.item.TextMenuItem;
import org.anddev.andengine.entity.scene.menu.item.decorator.ColorMenuItemDecorator;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.font.FontFactory;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.graphics.Color;
import android.view.KeyEvent;
import android.widget.Toast;

public class Andengine_Gamemenu_ExampleActivity extends BaseGameActivity
implements IOnMenuItemClickListener {
private static final int CAMERA_WIDTH = 800;
private static final int CAMERA_HEIGHT = 480;
private static final int MENU_ABOUT = 0;
private static final int MENU_QUIT = MENU_ABOUT + 1;
private static final int MENU_PLAY = MENU_QUIT + 1;
private static final int MENU_SCORES = MENU_PLAY + 1;
private static final int MENU_OPTIONS = MENU_SCORES + 1;
private static final int MENU_HELP = MENU_OPTIONS + 1;

// 照相机
private Camera mCamera;
// 主镜头
private Scene mMainScene;
// 菜单背景结构
private BitmapTextureAtlas mMenuBackTexture;
// 背景区域
private TextureRegion mMenuBackTextureRegion;
// 静态菜单镜头 弹出菜单镜头
private MenuScene mStaticMenuScene, mPopUpMenuScene;
// 弹出结构、字体结构
private BitmapTextureAtlas mPopUpTexture;
private Texture mFontTexture;
private Font mFont;

// 菜单结构区域
private TextureRegion mPopUpAboutTextureRegion;
private TextureRegion mPopUpQuitTextureRegion;
private TextureRegion mMenuPlayTextureRegion;
private TextureRegion mMenuScoresTextureRegion;
private TextureRegion mMenuOptionsTextureRegion;
private TextureRegion mMenuHelpTextureRegion;

// 弹出
private boolean popupDisplayed;

@Override
public void onLoadComplete() {
// TODO Auto-generated method stub

}

@Override
public Engine onLoadEngine() {
// TODO Auto-generated method stub
// 与加载图片一致
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() {
// TODO Auto-generated method stub
this.mFontTexture = new BitmapTextureAtlas(256, 256,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);
// 字体存放位置
FontFactory.setAssetBasePath("font/");
this.mFont = FontFactory.createFromAsset(mFontTexture, this,
"UnrealTournament.ttf", 32, true, Color.WHITE);
// 加载引擎管理结构
this.mEngine.getTextureManager().loadTexture(this.mFontTexture);
this.mEngine.getFontManager().loadFont(mFont);
// 图片存放位置
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
// 菜单背景
this.mMenuBackTexture = new BitmapTextureAtlas(1024, 512,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);

// 菜单背景区域,取得存放的资源
this.mMenuBackTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(this.mMenuBackTexture, this,
"snake_background.png", 0, 0);
this.mEngine.getTextureManager().loadTexture(mMenuBackTexture);

// 弹出结构
this.mPopUpTexture = new BitmapTextureAtlas(512, 512,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);

// 按菜单按钮弹出关于图片
this.mPopUpAboutTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(this.mPopUpTexture, this, "menu_ok.png", 0, 0);
// 按菜单按钮弹出退出图片
this.mPopUpQuitTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(this.mPopUpTexture, this, "menu_quit.png", 0, 50);
this.mEngine.getTextureManager().loadTexture(mPopUpTexture);
// 弹出设置为false
this.popupDisplayed = false;

}

@Override
public Scene onLoadScene() {
// TODO Auto-generated method stub
this.mEngine.registerUpdateHandler(new FPSLogger());
// 静态菜单和弹出菜单方法
this.createStaticMenuScene();
this.createPopUpMenuScene();
// 菜单设置到屏幕中心
final int centerX = (CAMERA_WIDTH - this.mMenuBackTextureRegion
.getWidth()) / 2;
final int centerY = (CAMERA_HEIGHT - this.mMenuBackTextureRegion
.getHeight()) / 2;

this.mMainScene = new Scene(1);

// 增加精灵和静态菜单
final Sprite menuBack = new Sprite(centerX, centerY,
this.mMenuBackTextureRegion);

this.mMainScene.getLastChild().attachChild(menuBack);
this.mMainScene.setChildScene(mStaticMenuScene);

return this.mMainScene;

}

// 创建弹出按钮镜头
private void createPopUpMenuScene() {
// TODO Auto-generated method stub
this.mPopUpMenuScene = new MenuScene(this.mCamera);

final SpriteMenuItem aboutMenuItem = new SpriteMenuItem(MENU_ABOUT,
this.mPopUpAboutTextureRegion);
aboutMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA,
GL10.GL_ONE_MINUS_SRC_ALPHA);

this.mPopUpMenuScene.addMenuItem(aboutMenuItem);

final SpriteMenuItem quitMenuItem = new SpriteMenuItem(MENU_QUIT,
this.mPopUpQuitTextureRegion);
quitMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA,
GL10.GL_ONE_MINUS_SRC_ALPHA);
this.mPopUpMenuScene.addMenuItem(quitMenuItem);

this.mPopUpMenuScene.setMenuAnimator(new SlideMenuAnimator());
this.mPopUpMenuScene.buildAnimations();
this.mPopUpMenuScene.setBackgroundEnabled(false);

this.mPopUpMenuScene.setOnMenuItemClickListener(this);

}

// 创建静态菜单方法
private void createStaticMenuScene() {
// TODO Auto-generated method stub
this.mStaticMenuScene = new MenuScene(this.mCamera);
final IMenuItem playMenuItem = new ColorMenuItemDecorator(
new TextMenuItem(MENU_PLAY, mFont, "开始游戏"), 0.5f, 0.5f, 0.5f,
1.0f, 0.0f, 0.0f);
playMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA,
GL10.GL_ONE_MINUS_SRC_ALPHA);
this.mStaticMenuScene.addMenuItem(playMenuItem);
final IMenuItem scoresMenuItem = new ColorMenuItemDecorator(
new TextMenuItem(MENU_SCORES, mFont, "分数"), 0.5f, 0.5f, 0.5f,
1.0f, 0.0f, 0.0f);
scoresMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA,
GL10.GL_ONE_MINUS_SRC_ALPHA);
this.mStaticMenuScene.addMenuItem(scoresMenuItem);

final IMenuItem optionsMenuItem = new ColorMenuItemDecorator(
new TextMenuItem(MENU_OPTIONS, mFont, "选项"), 0.5f, 0.5f, 0.5f,
1.0f, 0.0f, 0.0f);
optionsMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA,
GL10.GL_ONE_MINUS_SRC_ALPHA);
this.mStaticMenuScene.addMenuItem(optionsMenuItem);

final IMenuItem helpMenuItem = new ColorMenuItemDecorator(
new TextMenuItem(MENU_HELP, mFont, "帮助"), 0.5f, 0.5f, 0.5f,
1.0f, 0.0f, 0.0f);
helpMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA,
GL10.GL_ONE_MINUS_SRC_ALPHA);
this.mStaticMenuScene.addMenuItem(helpMenuItem);
this.mStaticMenuScene.buildAnimations();

this.mStaticMenuScene.setBackgroundEnabled(false);

this.mStaticMenuScene.setOnMenuItemClickListener(this);

}

@Override
public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
float pMenuItemLocalX, float pMenuItemLocalY) {
switch (pMenuItem.getID()) {
case MENU_ABOUT:
Toast.makeText(Andengine_Gamemenu_ExampleActivity.this, "关于",
Toast.LENGTH_SHORT).show();
return true;
case MENU_QUIT:

this.finish();
return true;
case MENU_PLAY:
Toast.makeText(Andengine_Gamemenu_ExampleActivity.this, "开始",
Toast.LENGTH_SHORT).show();
return true;
case MENU_SCORES:
Toast.makeText(Andengine_Gamemenu_ExampleActivity.this, "分数",
Toast.LENGTH_SHORT).show();
return true;
case MENU_OPTIONS:
Toast.makeText(Andengine_Gamemenu_ExampleActivity.this, "选项",
Toast.LENGTH_SHORT).show();
return true;
case MENU_HELP:
Toast.makeText(Andengine_Gamemenu_ExampleActivity.this,
"帮助", Toast.LENGTH_SHORT).show();
return true;
default:
return false;
}

}

// 菜单按钮按下事件
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_MENU
&& event.getAction() == KeyEvent.ACTION_DOWN) {
if (this.popupDisplayed) {
// 移除以及重新设置
this.mPopUpMenuScene.back();
// 设置子场景到静态场景
this.mMainScene.setChildScene(this.mStaticMenuScene);
this.popupDisplayed = false;
} else {
/* Attach the menu */
this.mMainScene.setChildScene(this.mPopUpMenuScene, false,
true, true);
this.popupDisplayed = true;
}
return true;
}
return super.onKeyDown(keyCode, event);
}

}

你可能感兴趣的:(AndEngine)