package lyu.edu.activity;
import java.io.IOException;
import 链接已屏蔽ponent.GameTouchEvent;
import lyu.edu.util.constants.JJYL;
import org.anddev.andengine.audio.sound.Sound;
import org.anddev.andengine.audio.sound.SoundFactory;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnAreaTouchListener;
import org.anddev.andengine.entity.scene.Scene.ITouchArea;
import org.anddev.andengine.entity.scene.background.AutoParallaxBackground;
import org.anddev.andengine.entity.scene.background.RepeatingSpriteBackground;
import org.anddev.andengine.entity.scene.background.ParallaxBackground.ParallaxEntity;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureManager;
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.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.opengl.texture.source.AssetTextureSource;
import org.anddev.andengine.util.HorizontalAlign;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;
public class GameMainActivity extends JJYLGameActivity implements
IOnAreaTouchListener{
private static final int BUTTON_WIDTH = 128;
private static final int BUTTON_HEIGHT = 64;
private Texture mBackgroundTexture;
private TextureRegion mBackgroud;
private Texture mButtonTexture;
private TiledTextureRegion mStartRegion;
private TiledTextureRegion mSettingsRegion;
private TiledTextureRegion mExitRegion;
private Texture mFontTexture;
private Font mButtonFont;
private Sound mButtonSound;
//加载游戏资源
public void onLoadResources() {
//获取纹理管理器
TextureManager textureManager = mEngine.getTextureManager();
//加载背景图片资源
mBackgroundTexture = new Texture(1024,512,TextureOptions.DEFAULT);
mBackgroud = TextureRegionFactory.createFromAsset(mBackgroundTexture, this, "gfx/start.jpg", 0, 0);
// 申请显示按钮所需的纹理资源
mButtonTexture = new Texture(BUTTON_WIDTH, BUTTON_HEIGHT * 2,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);
// 基于所申请的纹理资源构建按钮
AssetTextureSource source = new AssetTextureSource(this,
"gfx/button.png");
mStartRegion = TextureRegionFactory.createTiledFromSource(
mButtonTexture, source, 0, 0, 1, 2);
mSettingsRegion = TextureRegionFactory.createTiledFromSource(
mButtonTexture, source, 0, 0, 1, 2);
mExitRegion = TextureRegionFactory.createTiledFromSource(
mButtonTexture, source, 0, 0, 1, 2);
// 申请构建字体所需的纹理资源
mFontTexture = new Texture(BUTTON_WIDTH, BUTTON_HEIGHT,
TextureOptions.DEFAULT);
// 构建字体
mButtonFont = new Font(mFontTexture, Typeface.create(Typeface.DEFAULT,
Typeface.BOLD), 16, true, Color.WHITE);
// 将纹理资源加载到纹理管理器中
textureManager.loadTexture(mButtonTexture);
textureManager.loadTexture(mBackgroundTexture);
textureManager.loadTexture(mFontTexture);
// 加载字体资源
mEngine.getFontManager().loadFont(mButtonFont);
if (getGame().isSoundOn()) {
// 加载声音资源
SoundFactory.setAssetBasePath("sound/");
try {
mButtonSound = SoundFactory.createSoundFromAsset(getEngine()
.getSoundManager(), this, "Sound_5.mp3");
mButtonSound.setVolume(10f);
} catch (IOException e) {
Log.e("Game", e.getMessage());
}
}
}
//加载场景类
public Scene onLoadScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
//创建scene
Scene scene = new Scene();
//为scene创建背景
float StartGamePositionX= 50;
float StartGamePositionY = 270;
AutoParallaxBackground autoParallaxBackground = new AutoParallaxBackground(0, 0, 0, 5);
autoParallaxBackground.attachParallaxEntity(new ParallaxEntity(0.0f, new Sprite(0,
JJYL.CAMERA_HEIGHT - this.mBackgroud.getHeight(), this.mBackgroud)));
scene.setBackground(autoParallaxBackground);
// 创建按钮对象
AnimatedSprite spriteStart = new AnimatedSprite(StartGamePositionX, StartGamePositionY, BUTTON_WIDTH,
BUTTON_HEIGHT, mStartRegion);
spriteStart.setUserData(JJYL.START_GAME);
// 创建按钮文本
Text textStart = new Text(StartGamePositionX, StartGamePositionY, mButtonFont, JJYL.START_GAME,
HorizontalAlign.CENTER);
float textX = StartGamePositionX + (BUTTON_WIDTH - textStart.getWidthScaled()) / 2;
float textY =StartGamePositionY + (BUTTON_HEIGHT - textStart.getHeightScaled()) / 2;
textStart.setPosition(textX, textY);
StartGamePositionY += BUTTON_HEIGHT * 1.5f;
AnimatedSprite spriteSettings = new AnimatedSprite(StartGamePositionX, StartGamePositionY, BUTTON_WIDTH,
BUTTON_HEIGHT, mSettingsRegion);
spriteSettings.setUserData(JJYL.SETTINGS);
Text textSettings = new Text(StartGamePositionX, StartGamePositionY, mButtonFont, JJYL.SETTINGS,
HorizontalAlign.CENTER);
textX =StartGamePositionX + (BUTTON_WIDTH - textSettings.getWidthScaled()) / 2;
textY =StartGamePositionY + (BUTTON_HEIGHT - textSettings.getHeightScaled()) / 2;
textSettings.setPosition(textX, textY);
StartGamePositionY += BUTTON_HEIGHT * 1.5f;
AnimatedSprite spriteExit = new AnimatedSprite(StartGamePositionX, StartGamePositionY, BUTTON_WIDTH,
BUTTON_HEIGHT, mExitRegion);
spriteExit.setUserData(JJYL.EXIT);
Text textExit = new Text(StartGamePositionX, StartGamePositionY, mButtonFont, JJYL.EXIT,
HorizontalAlign.CENTER);
textX =StartGamePositionX + (BUTTON_WIDTH - textExit.getWidthScaled()) / 2;
textY =StartGamePositionY + (BUTTON_HEIGHT - textExit.getHeightScaled()) / 2;
textExit.setPosition(textX, textY);
// 注册按钮触发监听事件
scene.registerTouchArea(spriteStart);
// 将按钮和文本添加到Scene
scene.attachChild(spriteStart);
scene.attachChild(textStart);
scene.registerTouchArea(spriteSettings);
scene.attachChild(spriteSettings);
scene.attachChild(textSettings);
scene.registerTouchArea(spriteExit);
scene.attachChild(spriteExit);
scene.attachChild(textExit);
// 注册Touch事件监听器
scene.setOnAreaTouchListener(this);
return scene;
}
public void onLoadComplete() {
}
//释放资源
public void onUnLoadResources(){
super.onUnloadResources();
if(getGame().isSoundOn()){
if(mButtonSound != null){
//释放声音资源
mButtonSound.stop();
mButtonSound.release();
}
}
}
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
ITouchArea pTouchArea, float pTouchAreaLocalX,
float pTouchAreaLocalY) {
//如果按钮被按下
if(pSceneTouchEvent.isActionDown()){
//播放声音效果
if(getGame().isSoundOn()){
mButtonSound.play();
}
//播放动画效果
AnimatedSprite sprite = (AnimatedSprite)pTouchArea;
sprite.animate(new long[]{200,200,200,200},0,3,true);
// 通知游戏主控制类
getGame().notifyButtonTouched(
new GameTouchEvent(pSceneTouchEvent, pTouchArea));
}
return false;
}
}
更多源码 | 好库简介 | 网站地图 | 帮助中心 | 版权说明
Copyright© 2009-2012 OKBASE.NET All Rights Reserved 好库网 版权所有