打造轻量级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类即可。
Music:(与Globals没太大关系,因为篇幅比较小,所以写到一片博客里了)
新建文件夹Music,在里面添加一名为SAMusic的静态类。
音乐管理主要是处理音乐和音效,我们设置如下的几个变量:
方法的话,就是要注意
1.Setup()的时候会检测用户是否在听自己的音乐,如果是的话,它会将播放游戏音乐和音效的开关关闭。
2.播放音乐时,我们判断CanPlayMusic(),然后自己决定是否PlaySong()。
而播放音效时,我们不用判断,直接PlaySoundEffect()即可(会自己根据开关变量来选择是否播放)
3.方法的名字比较显而易见,大家试着调用一下差不多就了解了。
4.开发Windows Phone的同学要注意下,为了通过审核,我们的游戏在播放音乐之前一定要先判断用户是否在听他自己的音乐,如果是的话,我们可以:
a.询问他到底想听谁的,
b.不播放游戏的音乐(等他想听了,自己打开游戏的音乐设置)
代码的模块如下图:(写道这里发现,其实音调也可以设置的,但是漏掉了...)
这两块的内容比较少,在后文中会给出一个整合了更多模块的使用样例来,敬请期待。
(接下来不少类的命名都是以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 }
这两块的内容比较少,在后文中会给出一个整合了更多模块的使用样例来,敬请期待。
相关文章推荐
- 打造轻量级Windows Phone7 游戏引擎-Samurai 第二话(上) Inputs 问题提出篇
- 打造轻量级Windows Phone7 游戏引擎-Samurai 第五话 使用Samurai创建游戏
- 打造轻量级Windows Phone7 游戏引擎-Samurai 第二话(中) Inputs 问题解决篇一
- 打造轻量级Windows Phone7 游戏引擎-Samurai 第三话SADirector与SAScreen
- 打造轻量级Windows Phone7 游戏引擎-Samurai 第零话 前言
- 打造轻量级Windows Phone7 游戏引擎-Samurai 第四话 Button(上)
- 青瓷引擎之纯JavaScript打造HTML5游戏第二弹——《跳跃的方块》Part 7(服务器连接&数据处理)
- 青瓷引擎之纯JavaScript打造HTML5游戏第二弹——《跳跃的方块》Part 9(登陆等待&结算界面)
- 青瓷引擎之纯JavaScript打造HTML5游戏第二弹——《跳跃的方块》Part 10(排行榜界面&界面管理)
- 面向云安全 瑞星打造全球第一的杀病毒引擎
- 开源2D游戏引擎(Java&Android),LGame-0.3.2版正式发布
- 技术展示篇: 终于掌握了Torque游戏引擎的DTS和DSQ的制作,太高兴了(my success in DTS & DSQ)
- Mozilla,Epic携手打造Unreal 3 游戏引擎
- 2016网易游戏暑假实习第一场·第一题
- HTML5 2D游戏引擎研发系列 第四章 <Canvas技术篇-画布技术-基于手动切片动画>
- Unity打造简易的GalGame游戏剧本编辑引擎
- WEBGL 2D游戏引擎研发系列 第四章 <感想以及矩阵>
- 青瓷引擎之纯JavaScript打造HTML5游戏第二弹——《跳跃的方块》Part 5(添加微信支持)
- 开源3D&2D游戏开发引擎之GoDot简单介绍(一)
- 使用游戏引擎photon打造一款特殊的远程控制软件