斗破苍穹java_安卓斗破苍穹游戏源码

这篇博客详细介绍了Android游戏开发中资源的加载过程,包括背景图片、按钮纹理、字体和声音资源。同时,它展示了如何创建场景、设置背景、添加按钮及文本,并实现了触摸事件监听以响应用户操作。游戏主活动类`GameMainActivity`实现了按钮的点击动画和声音效果,以及与游戏主控制类的通信。
摘要由CSDN通过智能技术生成

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 好库网 版权所有

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值