Android游戏开发引擎AndEngine加载(游戏菜单)packagegongziba;imp
Android 游戏开发 引擎AndEngine加载(游戏菜单)
package gongziba;
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.andenginnu.MenuScene;
import org.anddev.andenginnu.MenuScene.IOnMenuItemClickListener;
import org.anddev.andenginnu.animator.SlideMenuAnimator;
import org.anddev.andenginnu.item.IMenuItem;
import org.anddev.andenginnu.item.SpriteMenuItem;
import org.anddev.andenginnu.item.TextMenuItem;
import org.anddev.andenginnu.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.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import android.graphics.Color;
import android.view.KeyEvent;
import android.widget.Toast;
public class MainMenuActivity extends
BaseGameActivity implements
IOnMenuItemClickListener {
// ===========================================================
// Constants
// ===========================================================
private static final int CAMERA_WIDTH = 410;
private static final int CAMERA_HEIGHT = 308;
protected static final int MENU_ABOUT = 0;
protected static final int MENU_QUIT = MENU_ABOUT + 1;
protected static final int MENU_PLAY = 100;
protected static final int MENU_SCORES = MENU_PLAY + 1;
protected static final int MENU_OPTIONS = MENU_SCORES + 1;
protected static final int MENU_HELP = MENU_OPTIONS + 1;
// 照相机
protected Camera mCamera;
// 主镜头
protected Scene mMainScene;
// 菜单背景结构
private Texture mMenuBackTexture;
// 背景区域
private TextureRegion mMenuBackTextureRegion;
// 静态菜单镜头
protected MenuScene mStaticMenuScene,
mPopUpMenuScene;
// 弹出结构、字体结构
private Texture mPopUpTexture;
private Texture mFontTexture;
private Font mFont;
// 菜单结构区域
protected TextureRegion mPopUpAboutTextureRegion;
protected TextureRegion mPopUpQuitTextureRegion;
protected TextureRegion mMenuPlayTextureRegion;
protected TextureRegion mMenuScoresTextureRegion;
protected TextureRegion mMenuOptionsTextureRegion;
protected TextureRegion mMenuHelpTextureRegion;
// 弹出
private boolean popupDisplayed;
// 加载引擎场景
@Override
public Engine onLoadEngine() {
// 与加载图片一致
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() {
/* Load Font/Textures. */
this.mFontTexture = new Texture(
256,
256,
// 结构选项设置
TextureOptions.BILINEAR_PREMULTIPLYALPHA);
// 字体存放位置
FontFactory
.setAssetBasePath("gfx/font/");
// 获取字体
this.mFont = FontFactory
.createFromAsset(
this.mFontTexture,
this,
"Flubber.ttf",
32, true,
Color.WHITE);
// 加载引擎管理结构
this.mEngine
.getTextureManager()
.loadTexture(
this.mFontTexture);
this.mEngine.getFontManager()
.loadFont(this.mFont);
// 菜单背景
this.mMenuBackTexture = new Texture(
512,
512,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);
// 菜单背景区域 取得存放的资源
this.mMenuBackTextureRegion = TextureRegionFactory
.createFromAsset(
this.mMenuBackTexture,
this,
"gfx/icon.jpg",
0, 0);
this.mEngine
.getTextureManager()
.loadTexture(
this.mMenuBackTexture);
// 弹出结构
this.mPopUpTexture = new Texture(
512,
512,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);
// 按菜单按钮弹出关于 图片
this.mPopUpAboutTextureRegion = TextureRegionFactory
.createFromAsset(
this.mPopUpTexture,
this,
"gfx/MainMenu/About_button.png",
0, 0);
// 按菜单按钮弹出退出 图片
this.mPopUpQuitTextureRegion = TextureRegionFactory
.createFromAsset(
this.mPopUpTexture,
this,
"gfx/MainMenu/Quit_button.png",
0, 50);
this.mEngine
.getTextureManager()
.loadTexture(
this.mPopUpTexture);
// 弹出设置为false
popupDisplayed = false;
}
// 加载镜头
@Override
public Scene onLoadScene() {
// 注册更新线程
// import org.anddev.andengine.entity.util.FPSLogger;
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);
mMainScene.getLastChild()
.attachChild(menuBack);
mMainScene
.setChildScene(mStaticMenuScene);
return this.mMainScene;
}
@Override
public void onLoadComplete() {
}
// 菜单按钮按下事件
@Override
public boolean onKeyDown(
final int pKeyCode,
final KeyEvent pEvent) {
if (pKeyCode == KeyEvent.KEYCODE_MENU
&& pEvent.getAction() == KeyEvent.ACTION_DOWN) {
if (popupDisplayed) {
// 移除以及重新设置
this.mPopUpMenuScene
.back();
// 朱场景设置子场景到静态场景
mMainScene
.setChildScene(mStaticMenuScene);
popupDisplayed = false;
} else {
/* Attach the menu. */
this.mMainScene
.setChildScene(
this.mPopUpMenuScene,
false,
true,
true);
popupDisplayed = true;
}
return true;
} else {
return super.onKeyDown(
pKeyCode, pEvent);
}
}
@Override
public boolean onMenuItemClicked(
final MenuScene pMenuScene,
final IMenuItem pMenuItem,
final float pMenuItemLocalX,
final float pMenuItemLocalY) {
switch (pMenuItem.getID()) {
case MENU_ABOUT:
Toast.makeText(
MainMenuActivity.this,
"关于",
Toast.LENGTH_SHORT)
.show();
return true;
case MENU_QUIT:
this.finish();
return true;
case MENU_PLAY:
Toast.makeText(
MainMenuActivity.this,
"开始",
Toast.LENGTH_SHORT)
.show();
return true;
case MENU_SCORES:
Toast.makeText(
MainMenuActivity.this,
"分数",
Toast.LENGTH_SHORT)
.show();
return true;
case MENU_OPTIONS:
Toast.makeText(
MainMenuActivity.this,
"选项",
Toast.LENGTH_SHORT)
.show();
return true;
case MENU_HELP:
Toast.makeText(
MainMenuActivity.this,
"Help selected",
Toast.LENGTH_SHORT)
.show();
return true;
default:
return false;
}
}
// 静态菜单方法
protected void createStaticMenuScene() {
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);
}
//创建弹出按钮镜头
protected void createPopUpMenuScene() {
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);
}
}