您的位置:首页 > 大数据 > 人工智能

打造轻量级Windows Phone7 游戏引擎-Samurai 第一话 Globals & Music

2013-10-10 20:48 423 查看
我们先建一个“Windows Phone游戏”项目名为SaDemo,然后在解决方案中添加一个新建项目“Windows Phone游戏库”,命名为Samurai,最后别忘了给SaDemo添加Samurai的引用。

(接下来不少类的命名都是以SA开头~)

Globals:(在Samurai中新建文件夹Globals,然后添加一个类,命名为SAGlobal)

正常的在XNA Game中我们总是会用到如下几个引用:game,spriteBatch,Content等。为了方便使用,我们专门写一个静态类SAGlobal来在全局使用这些变量。

当然了,spriteBatch是在LoadContent()方法中初始化的,所以,我们在LoadContent()中初始化SAGlobal类即可。

public  static class SAGlobal
{
public static Game game;
public  static ContentManager Content;
public  static SpriteBatch spriteBatch;
public static GraphicsDeviceManager graphics;

public static void Setup(Game game, SpriteBatch spriteBatch, GraphicsDeviceManager graphics)
{
SAGlobal.game = game;
SAGlobal.Content = game.Content;
SAGlobal.spriteBatch = spriteBatch;
SAGlobal.graphics = graphics;
}
}


Music:(与Globals没太大关系,因为篇幅比较小,所以写到一片博客里了)

新建文件夹Music,在里面添加一名为SAMusic的静态类。

音乐管理主要是处理音乐和音效,我们设置如下的几个变量:

//音乐/音效集合
static Dictionary<string, Song> songList = new Dictionary<string, Song>();
static Dictionary<string, SoundEffect> soundList = new Dictionary<string, SoundEffect>();
//依赖项 用于加载
private static ContentManager Content;
//标志
private static bool IfPlayMusic;
private static bool IfPlaySound;
private static float songVolume;
private static float soundEffectVolume;

方法的话,就是要注意

1.Setup()的时候会检测用户是否在听自己的音乐,如果是的话,它会将播放游戏音乐和音效的开关关闭。

2.播放音乐时,我们判断CanPlayMusic(),然后自己决定是否PlaySong()。

而播放音效时,我们不用判断,直接PlaySoundEffect()即可(会自己根据开关变量来选择是否播放)

3.方法的名字比较显而易见,大家试着调用一下差不多就了解了。

4.开发Windows Phone的同学要注意下,为了通过审核,我们的游戏在播放音乐之前一定要先判断用户是否在听他自己的音乐,如果是的话,我们可以:

a.询问他到底想听谁的,

b.不播放游戏的音乐(等他想听了,自己打开游戏的音乐设置)

代码的模块如下图:(写道这里发现,其实音调也可以设置的,但是漏掉了...)



public static class SAMusicManager
{
//音乐/音效集合
static Dictionary<string, Song> songList = new Dictionary<string, Song>();
static Dictionary<string, SoundEffect> soundList = new Dictionary<string, SoundEffect>();
//依赖项 用于加载
private static ContentManager Content;
//标志
private static bool IfPlayMusic;
private static bool IfPlaySound;
private static float songVolume;
private static float soundEffectVolume;

//使用前需要先注册给SADirector
public static void Setup(ContentManager content)
{
SAMusicManager.Content = content;
IfPlaySound = IfPlayMusic = CanPlayMusic();
SetVolume(1);
}

public static void Setup()
{
Setup(SAGlobal.Content);
}

#region Music总开关
public static void DisableAll()
{
EnableSoundEffect(false);
if (CanPlayMusic())
{
StopSong();
}
}

public static void EnableAll(string name)
{
PlaySong(name);
EnableSoundEffect(true);
}
#endregion

#region 播放音乐相关
public static void PlaySong(string name)
{
PlaySong(name,true);
}
public static void PlaySong(string name, bool ifLoop)
{
if (!songList.ContainsKey(name))
{
songList.Add(name, Content.Load<Song>(name));
}
MediaPlayer.IsRepeating = ifLoop;
MediaPlayer.Volume = songVolume;
MediaPlayer.Play(songList[name]);
IfPlayMusic = true;
}
public static void RemoveSong(string name)
{
if (songList.ContainsKey(name))
{
songList.Remove(name);
}
}
public static bool CanPlayMusic()
{
return MediaPlayer.GameHasControl;
}
public static void PauseSong()
{
MediaPlayer.Pause();
IfPlayMusic = false;
}
public static void StopSong()
{
MediaPlayer.Stop();
IfPlayMusic = false;
}
public static void ResumeSong()
{
MediaPlayer.Resume();
IfPlayMusic = true;
}
#endregion

#region 播放音效相关
public static void EnableSoundEffect(bool soundOn)
{
SAMusicManager.IfPlaySound = soundOn;
}
public static void PlaySoundEffect(string name)
{
if (IfPlaySound)
{
if (!soundList.ContainsKey(name))
{
soundList.Add(name, Content.Load<SoundEffect>(name));
}
SoundEffectInstance temp = soundList[name].CreateInstance();
temp.Volume = soundEffectVolume;
temp.Play();
}
}
public static void RemoveSoundEffect(string name)
{
if (soundList.ContainsKey(name))
{
soundList.Remove(name);
}
}
#endregion

#region 设置音量大小
public static void SetVolume(float v)
{
SetSongVolume(v);
SetSoundEffectVolume(v);
}

public static void SetSongVolume(float v)
{
if (v <= 1.0&&v>=0.0)
{
songVolume = v;
}
}

public static void SetSoundEffectVolume( float v)
{
if (v <= 1.0&&v>=0.0)
{
soundEffectVolume = v;
}
}
#endregion
}


这两块的内容比较少,在后文中会给出一个整合了更多模块的使用样例来,敬请期待。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐