斗破苍穹java_安卓斗破苍穹游戏源码
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 好庫網 版權所有
總結
以上是生活随笔為你收集整理的斗破苍穹java_安卓斗破苍穹游戏源码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: APUE学习笔记-15章进程间通信
- 下一篇: C/C++:实现象棋游戏