<a href="http://www.jianshu.com/p/e81f4282687b">之前</a>已经构建好项目,所以今天直接就是开发过程了,这一篇内容和Canavs并没有什么关系,主要是保证项目的合理性,前期模拟一个游戏的流程。
1.创建一个MainActivity,界面主要有一些基本操作
这里有2个战机提供选择,选中的携带一个动画效果,实现思路。
1.等待布局初始化完成后默认选中第一个战机
2.打开游戏的时候就有背景音乐播放,这里我们的音乐播放主要实现的类<b>MediaPlayer</b>
创建一个GameMusic的单例模式的类
package com.tangyx.game.util;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
/**
* Create tangyx 2016/12/20
* 背景音乐
*/
public class GameMusic implements OnCompletionListener{
private static GameMusic INSTANCE;
private Context mContext;
private MediaPlayer mMediaPlayer;
private boolean isResume;
private boolean isPlay;
public static GameMusic getInstance(Context context){
if(INSTANCE==null){
INSTANCE = new GameMusic(context);
}
return INSTANCE;
}
private GameMusic(Context ac) {
this.mContext = ac;
mMediaPlayer = new MediaPlayer();
}
/**
* 准备好播放的音乐资源
* @param musicName
*/
public void prepareMediaPlayer(String musicName){
mMediaPlayer.setOnCompletionListener(this);
AssetFileDescriptor descriptor;
try {
//音乐文件在assets文件夹下
descriptor = mContext.getAssets().openFd("dj/"+musicName);
if(descriptor!=null){
mMediaPlayer.reset();
mMediaPlayer.setDataSource(descriptor.getFileDescriptor(),descriptor.getStartOffset(),descriptor.getDeclaredLength());
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
descriptor.close();
mMediaPlayer.prepare();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 播放音乐
* @param loop
*/
public void play(boolean loop){
if(mMediaPlayer !=null){
mMediaPlayer.setLooping(loop);
mMediaPlayer.start();
isPlay =true;
}
}
/**
* 停止音乐
*/
public void stop(){
if(mMediaPlayer !=null&& isPlay){
mMediaPlayer.stop();
}
}
/**
* 暂停音乐
*/
public void pause(){
if(mMediaPlayer !=null&& isPlay){
mMediaPlayer.pause();
}
}
/**
* 暂停后继续播放
*/
public void start(){
if(mMediaPlayer !=null){
mMediaPlayer.start();
isPlay =true;
}
}
public void restart(){
mMediaPlayer.seekTo(0);
}
public void onResume(){
if(mMediaPlayer !=null){
if(isResume){
mMediaPlayer.start();
isResume = false;
}
}
}
@Override
public void onCompletion(MediaPlayer mp) {
/**
* 释放资源
*/
mp.release();
}
}
然后在onCreate直接调用即可
GameMusic.getInstance(this).prepareMediaPlayer(getString(R.string.menumusic));
GameMusic.getInstance(this).play(true);
3.点击游戏开始或者其他按钮的时候有一个点击动画以及点击音效
创建一个点击翻转的动画类GameScaleAnimation,动画类的翻转主要实现<b>ScaleAnimation</b>
package com.tangyx.game.util;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
/**
* 按钮点击反转的动画效果。
*/
public class GameScaleAnimation implements AnimationListener{
public ScaleAnimation scale0;
public ScaleAnimation scale1;
private View iv;
private boolean bol;
private int duration;
public GameScaleAnimation(int duration) {
scale0 = new ScaleAnimation(1, 0, 1, 1, 1, 0.5f, 1, 0);
scale1 = new ScaleAnimation(0, 1, 1, 1, 1, 0.5f,100, 0);
scale0.setDuration(duration);
scale1.setDuration(duration);
scale0.setAnimationListener(this);
scale1.setAnimationListener(this);
this.duration = duration;
}
/**
*
* @param duration 动画执行的时常
* @param isBol 是否执行结束
*/
public GameScaleAnimation(int duration,boolean isBol) {
scale0 = new ScaleAnimation(1, 0, 1, 1, 1, 0.5f, 1, 0);
scale1 = new ScaleAnimation(0, 1, 1, 1, 1, 0.5f,100, 0);
scale0.setDuration(duration);
scale1.setDuration(duration);
scale0.setAnimationListener(this);
scale1.setAnimationListener(this);
this.duration = duration;
this.bol = isBol;
}
public void startAnimation(View civ){
stopAnimation();
iv = civ;
iv.startAnimation(scale0);
}
public void stopAnimation(){
if(iv!=null){
iv.clearAnimation();
}
}
/**
* 动画执行完成
* @param animation
*/
@Override
public void onAnimationEnd(Animation animation) {
if(!bol){
if(animation.hashCode()==scale0.hashCode()){
iv.startAnimation(scale1);
}else{
iv.startAnimation(scale0);
}
}else{
if(animation.hashCode()==scale0.hashCode()){
iv.startAnimation(scale1);
}else{
scale0.setDuration(duration);
scale1.setDuration(duration);
iv.startAnimation(scale0);
iv.clearAnimation();
bol = false;
}
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationStart(Animation animation) {
}
public boolean isBol() {
return bol;
}
public void setBol(boolean bol) {
this.bol = bol;
}
}
然后可以这样使用:
4.点击按钮的时候发出的声音,创建一个GameSoundPool类,主要实现<b>GameSoundPool</b>
package com.tangyx.game.util;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
/**
* 点击操作的音效
*/
public class GameSoundPool {
public static GameSoundPool INSTANCE;
public final static int LAUNCHER=0;
public final static int PLAYERBULLET=1;
public final static int GAMECLICK=2;
public final static int ENEMYCLEARA=3;
public final static int ENEMYCLEARB=4;
public final static int ENEMYCLEARC=5;
public int mVolume =15;
private SoundPool mSoundPool;
private Context mContext;
private int dex;
private Map<Integer, Integer> mSoundMap;
public static GameSoundPool getInstance(Context context){
if(INSTANCE==null){
INSTANCE = new GameSoundPool(context);
}
return INSTANCE;
}
private GameSoundPool(Context context) {
mSoundPool = new SoundPool(10,AudioManager.STREAM_MUSIC, 100);
mSoundMap = new HashMap<>();
this.mContext = context;
}
/**
* add music
*/
public void addMusic(int id,int resId,int priority){
mSoundMap.put(id, mSoundPool.load(mContext, resId, priority));
}
/**
*
* @param soundId 播发音乐的编号ID map的key
* @param priority 优先级别 多个音乐一起播发的时候
*/
public void play(int soundId,int priority){
mSoundPool.play(mSoundMap.get(soundId), mVolume, mVolume, priority, 0, 1f);
}
public void clear(){
if(mSoundMap.size()>0){
while(dex< mSoundMap.size()){
if(mSoundMap.get(dex)!=null){
mSoundPool.unload(mSoundMap.get(dex));
}
dex++;
}
}
}
}
使用方式:
先加入点击的音效文件
GameSoundPool.getInstance(this).addMusic(GameSoundPool.GAMECLICK, R.raw.click,0);
触发音效
GameSoundPool.getInstance(this).play(GameSoundPool.GAMECLICK,100);
今天这一篇主要就是实现这几个工具类了,其余部分都是很简单的布局,包括一些移动动画都是直接通过API直接实现的。
SceneActivity模拟选择场景
GameMapActivityView模拟选择游戏背景
说了这么多,没有源码那就是耍流氓,最后附上<a href="https://github.com/tangyxgit/GameCanvas">源码</a>
<b>说明:源码是通过git持续更新的,每一个模块或者每一篇文章讲解完成后更新上传,所以学习是一个漫长的过程,如果你喜欢这个过程,那就一起来学习。</b>
<a href="http://www.jianshu.com/p/e81f4282687b">上一篇</a> <a href="http://www.jianshu.com/p/05aa77075fe5" style="">下一篇</a>