您的位置:首页 > 其它

[翻译]XNA 3.0 Game Programming Recipes之sixteen

2009-07-25 22:56 253 查看

4 this.menuTite=menuTitle;
5 this.backgroundImage=backgroundImage;
6 } 显示标题应该容易。然而,绘制背景图片很麻烦,如果您使用不同的菜单背景图片。你想要的是,在这两个[b]Active和Ending
状态,图像显示。当在Starting模式,新的背景图像与前面的图象混合。当混合第二图像和第一个图像,您需要确认您的第一个图象实际上是第一个绘制的!做到这一点并不容易, 因为这将涉及改变菜单绘制顺序。
一个简单的方法是使用SpriteBatch.Draw方法的layerDepth参数。当在Active或Ending模式下,图片将会在距离1绘制,"deepest"层。在Starting模式,图象将会在0.5f深度绘制,所有的文本将会在距离为0处绘制。当使用SpriteSortMode.BackToFront,首先Active到Ending菜单在深度为1处将被绘制出。下一步,如果可以适用,Starting菜单将会被绘制(混合的图象已经存在了),最后所有文字被渲染在你的MenuWindow类的Draw方法,跟踪这两个变量。
1 float bgLayerDepth;
2 Color bgColor; 这里包含有背景图象的layerDepth和渐变的值,它设置有开关的模式:
1 switch(windowState)
2 {
3 case WindowState.Starting:
4 horPosition-=200*(1.0f-(float)smoothedProgress);
5 alphaValue=smoothedProgress;
6 bgLayerDepth=0.5f;
7 bgColor=new Color(new Vector4(1,1,1,alphaValue));
8 break;
9 case WindowState.Ending:
10 horPosition+=200*(float)smoothedProgress;
11 alphaValue=1.0f-smoothedProgress;
12 bgLayerDepth=1f;
13 bgColor=Color.White;
14 break;
15 default:
16 alphaValue=1;
17 bgLayerDepth=1;
18 bgColor=Color.White;
19 break;
20 } Color.WhiteColor(new Vector4(1,1,1,1))颜色一样,意思是说alpha值最大。如果一个菜单是Starting或者Ending状态,alphaValue被计算。下一步,你使用这个渐变的值去绘制标题和绘制背景图象。
1 Color titleColor=new Color(new Vector4(1,1,1,alphaValue));
2 spriteBatch.Draw(backgroundImage,new Vector2(),null,bgColor,0,Vector2.Zero,1,SpriteEffects.None,bgLayerDepth);
3 spriteBatch.DrawString(spriteFont,menuTilte,new Vector2(horPosition,200),titleColor,0,Vector2.Zero,1.5f,SpriteEffects.None,0); 你可以看见这个标题被1.5f倍缩放,这将会使它比正常菜单项目看上去大了。
最后,你需要确认你设置的主程序的Draw方法
1 //SpriteSortMode 到BackToFront:
2 spriteBatch.Begin(SpriteBlendMode.AlphaBlend,SpriteSortMode.BackToFront,SaveStateMode.None);
从菜单到游戏
在这一点上,您可以创建了一些不错的前瞻性菜单,但你怎么能真正的用菜单项目启动游戏?这项工作可以使用虚拟菜单完成,它需要储存在主程序。例如,如果您想启动新游戏菜单包含项目启动一种简单,一个正常,或一场非常难的游戏,加上这些菜单:
1 MenuWindow startGameEasy;
2 MenuWindow startGameNormal;
3 MenuWindow startGameHard;
4 bool menusRunning; 在你的LoadContent方法中,你可以实例化这些变量并且没有参数连接到项目在menuNewGame
1 startGameEasy=new MenuWindow(null,null,null);
2 startGameNormal=new MenuWindow(null,null,null);
3 startGameHard=new MenuWindow(null,null,null);
4 menuNewGame.AddMenuItem("Easy",startGameEasy);
5 menuNewGame.AddMenuItem("Normal",startGameNormal);
6 menuNewGame.AddMenuItem("Hard",startGameHard);
7 menuNewGame.AddMenuItem("Back to Main menu",menuMain); 这四个项目将新增到新的游戏菜单中。您需要做的下一步是检测是否任一虚拟菜单已经选定。因此,扩展您的MenuInput方法:
1 private void MenuInput(KeyboardState currentKeybState)
2 {
3 MenuWindow newActive=activeMenu.ProcessInput(lastKeybState,currentKeybState);
4 if(newActive==startGameEasy)
5 {
6 //set level to easy
7 menusRunning=false;
8 }
9 else if(newActive==startGameNormal)
10 {
11 //set level to normal
12 menusRunning=false;
13 }
14 else if(newActive==startGameHard)
15 {
16 //set level to hard
17 menusRunning=false;
18 }
19 else if(newActive==null)
20 this.Exit();
21 else if(newActive!=activeMenu)
22 newActive.WakeUp();
23 activeMenu=newActive;
24 } 当用户在游戏中时,你可以使用menusRunning变量确保你不会更新/绘制你菜单:
1 if(menusRunning)
2 {
3 spriteBatch.Begin(SpriteBlendMode.AlphaBlend,SpriteSortMode.BackToFront,SaveStateMode.None);
4 foreach(MenuWindow currentMenu in menuList)
5 currentMenu.Draw(spriteBatch);
6 spriteBatch.End();
7 Window.Title="Menu running..";
8 }
9 else
10 {
11 window.Title="Game running.";
12 }
代码

略,参看上面的代码!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: