本节在上一节基础上继续完善该游戏引擎,主要完成以下任务:

(1)完善StartMenuScreen,同时添加一个GamePlayScreen页面

(2)创建一个新的控件,picture box

(3)为ControlManager类添加新的事件处理

首先是给ControlManager类添加新的事件处理,FocusChanged事件;因为要通过LinkLabel实现页面间跳转,控件的聚焦变化后触发该事件,ControlManager类代码修改如下:

#region Event Region
public event EventHandler FocusChanged;//聚焦更改事件
#endregion
public void NextControl()
{
if (Count == 0)
return;
int currentControl = selectedControl;
this[selectedControl].HasFocus = false;
do{selectedControl++;
if (selectedControl == Count)selectedControl = 0;
if (this[selectedControl].TabStop && this[selectedControl].Enabled){//代码增添部分,当LinkLabel控件聚焦发生改变,触发事件
if (FocusChanged != null)FocusChanged(this[selectedControl], null);
break;}} while (currentControl != selectedControl);
this[selectedControl].HasFocus = true;
}public void PreviousControl()
{
if (Count == 0)
return;
int currentControl = selectedControl;
this[selectedControl].HasFocus = false;
do{selectedControl--;
if (selectedControl < 0)selectedControl = Count - 1;
if (this[selectedControl].TabStop && this[selectedControl].Enabled){
if (FocusChanged != null)FocusChanged(this[selectedControl], null);
break;}} while (currentControl != selectedControl);
this[selectedControl].HasFocus = true;
}

添加一个新的游戏页面GamePlayScreen,代码跟上一节的StartMenuScreen的代码相似,只是对BaseGameState的简单继承

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XRpgLibrary;
namespace EyesOfTheDragon.GameScreens
{
public class GamePlayScreen : BaseGameState{#region Field Region#endregion#region Property Region#endregion#region Constructor Region
public GamePlayScreen(Game game, GameStateManager manager): base(game, manager){}#endregion #region XNA Method Region
public override void Initialize(){
base.Initialize();}
protected override void LoadContent(){
base.LoadContent();}
public override void Update(GameTime gameTime){
base.Update(gameTime);}
public override void Draw(GameTime gameTime){
base.Draw(gameTime);}#endregion#region Abstract Method Region#endregion}
}

将该页面添加到Game1.cs中

public GamePlayScreen GamePlayScreen;//定义一个类级变量
public Game1()
{graphics = new GraphicsDeviceManager(this);graphics.PreferredBackBufferWidth = screenWidth;graphics.PreferredBackBufferHeight = screenHeight;ScreenRectangle = new Rectangle(0,0,screenWidth,screenHeight);Content.RootDirectory = "Content";Components.Add(new InputHandler(this));stateManager = new GameStateManager(this);Components.Add(stateManager);TitleScreen = new TitleScreen(this, stateManager);StartMenuScreen = new StartMenuScreen(this, stateManager);GamePlayScreen = new GamePlayScreen(this, stateManager);//在类中添加对象stateManager.ChangeState(TitleScreen);
}

第二件事就是创建一个新的控件:picture box,跟LinkLabel一样

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace XRpgLibrary.Controls
{
public class PictureBox : Control{#region Field Region
Texture2D image;//控件中加载的图片
Rectangle sourceRect;//图片的长度矩形
Rectangle destRect;//图片在窗体上呈现的矩形#endregion#region Property Region
public Texture2D Image{
get { return image; }
set { image = value; }}
public Rectangle SourceRectangle{
get { return sourceRect; }
set { sourceRect = value; }}
public Rectangle DestinationRectangle{
get { return destRect; }
set { destRect = value; }}#endregion#region Constructors构造函数的重载
public PictureBox(Texture2D image, Rectangle destination){Image = image;DestinationRectangle = destination;SourceRectangle = new Rectangle(0, 0, image.Width, image.Height);//原图片大小Color = Color.White;}
public PictureBox(Texture2D image, Rectangle destination, Rectangle source){Image = image;DestinationRectangle = destination;SourceRectangle = source;Color = Color.White;}#endregion#region Abstract Method Region//来自于继承类的方法重载
public override void Update(GameTime gameTime){ }
public override void Draw(SpriteBatch spriteBatch){spriteBatch.Draw(image, destRect, sourceRect, Color);}
public override void HandleInput(PlayerIndex playerIndex){}#endregion#region Picture Box Methods//为新的图片框设定位置
public void SetPosition(Vector2 newPosition){destRect = new Rectangle((int)newPosition.X,(int)newPosition.Y,sourceRect.Width,sourceRect.Height);}#endregion}
}

PictureBox控件需要加载图片,在EyesOfTheDragonContent目录下添加一个新的文件夹GUI,将leftarrowUp.png,rightarrowUp.png,StopBar.png三幅图片加入以便用于图片控件的调用

接下来就是最后一个任务,将PictureBox控件添加到StartMenuGreen.cs中来完善该页面,具体代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using XRpgLibrary;
using XRpgLibrary.Controls;
namespace EyesOfTheDragon.GameScreens
{
public class StartMenuScreen : BaseGameState{#region Field region
PictureBox backgroundImage;//页面中的背景图片
PictureBox arrowImage;//页面中的箭头图标
LinkLabel startGame;//三个选项,开始游戏,登陆游戏,结束游戏
LinkLabel loadGame;
LinkLabel exitGame;
float maxItemWidth = 0f;//用来控制箭头图标显示位置#endregion#region Property Region#endregion#region Constructor Region
public StartMenuScreen(Game game, GameStateManager manager): base(game, manager){}#endregion#region XNA Method Region
public override void Initialize(){
base.Initialize();}
protected override void LoadContent(){
base.LoadContent();//要先调用基类的登陆函数,这样就实例化了ControlManager对象
ContentManager Content = Game.Content; //backgroundImage = new PictureBox(Content.Load<Texture2D>(@"Backgrounds\titlescreen"), GameRef.ScreenRectangle);//实例化背景图片框并加入到控件管理对象中ControlManager.Add(backgroundImage);
Texture2D arrowTexture = Content.Load<Texture2D>(@"GUI\leftarrowUp");arrowImage = new PictureBox(arrowTexture,
new Rectangle(0,0,arrowTexture.Width,arrowTexture.Height));ControlManager.Add(arrowImage);//实例化箭头图片框并加入到控件管理对象中startGame = new LinkLabel();startGame.Text = "The story begins";startGame.Size = startGame.SpriteFont.MeasureString(startGame.Text);startGame.Selected +=new EventHandler(menuItem_Selected);ControlManager.Add(startGame);loadGame = new LinkLabel();loadGame.Text = "The story continues";loadGame.Size = loadGame.SpriteFont.MeasureString(loadGame.Text);loadGame.Selected += menuItem_Selected;ControlManager.Add(loadGame);exitGame = new LinkLabel();exitGame.Text = "The story ends";exitGame.Size = exitGame.SpriteFont.MeasureString(exitGame.Text);exitGame.Selected += menuItem_Selected;ControlManager.Add(exitGame);//在控件管理类中添加三个选择按键ControlManager.NextControl();ControlManager.FocusChanged += new EventHandler(ControlManager_FocusChanged);//订阅焦点改变事件
Vector2 position = new Vector2(350, 500);//定义选择按键的起始位置//遍历控件管理类中的所有控件
foreach (Control c in ControlManager){
if (c is LinkLabel){//如果是LinkLabel控件,就在起始位置上向下排列放置
if (c.Size.X > maxItemWidth)maxItemWidth = c.Size.X;c.Position = position;position.Y += c.Size.Y + 5f;}}ControlManager_FocusChanged(startGame, null);//触发事件,起始状态为箭头出现在startGame按键旁}
void ControlManager_FocusChanged(object sender, EventArgs e)//聚焦改变事件,为箭头标记重绘显示位置{
Control control = sender as Control;
Vector2 position = new Vector2(control.Position.X + maxItemWidth + 10f,
control.Position.Y);arrowImage.SetPosition(position);}
private void menuItem_Selected(object sender, EventArgs e){//按键选中事件触发,如果选中startGame或者loadGame按键,跳转到GamePlayScreen页面
if (sender == startGame){StateManager.PushState(GameRef.GamePlayScreen); }
if (sender == loadGame){StateManager.PushState(GameRef.GamePlayScreen);}//如果选中的是exitGame按键,跳出游戏
if (sender == exitGame){GameRef.Exit();}}
public override void Update(GameTime gameTime){ControlManager.Update(gameTime, playerIndexInControl);
base.Update(gameTime);}
public override void Draw(GameTime gameTime){GameRef.SpriteBatch.Begin();
base.Draw(gameTime);ControlManager.Draw(GameRef.SpriteBatch);GameRef.SpriteBatch.End();}#endregion#region Game State Method Region#endregion}
}

OK,今天任务结束

转载于:https://www.cnblogs.com/zcftech/archive/2013/04/03/2998293.html

XNA之RPG游戏开发教程之三相关推荐

  1. 仙剑奇侠传 游戏 开发 教程 Xianjian qixia development Game development tutorial

    仙剑奇侠传 开发  游戏 开发 教程 Xianjian qixia development Game development tutorial 作者:韩梦飞沙 Author:han_meng_fei_ ...

  2. Windows8 游戏开发教程-开篇

    准备开始写一个win8游戏开发教程了,现在这里整理一下思路. 需要在开篇解释的问题就有一大堆了,先尝试一个个把他们写下来. 1.阅读这个教程需要什么样的基础? 2.这个教程能带来一个怎样的DEMO,结 ...

  3. 简单的RPG游戏制作教程

    □企划部份 ◎第一步:决定资料格式 在进入游戏制作的初期,由于有许多和程式有关的资料需要编整,因此担任企划的人员常会忙得乱七八糟.在这个阶段,企划人员必需要和程式商量游戏中资料的格式.举个例子来说,在 ...

  4. 游戏开发教程-一名小白的开始

    游戏开发教程-一名小白的开始 游戏开发是一个过程,为了激发玩家玩游戏热情,通过遵循设计制作规则,来设计人物.场景等游戏要素.游戏开发现在主要分为PC端游戏开发和移动端游戏开发,无论是PC端还是移动端, ...

  5. Unity 2D游戏开发教程之摄像头追踪功能

    Unity 2D游戏开发教程之摄像头追踪功能 上一章,我们创建了一个简单的2D游戏.此游戏中的精灵有3个状态:idle.left和right.这看起来确实很酷!但是仅有的3个状态却限制了精灵的能力,以 ...

  6. 微信小游戏开发教程-游戏实现3

    微信小游戏开发教程-游戏实现3 对象池 由于游戏过程中会创建很多临时对象,这些对象很快又不再使用,垃圾回收器也能帮我们主动回收这部分垃圾,但是回收时间不可控制,同时增大了创建对象的开销,所以我们使用对 ...

  7. 微信小游戏开发教程-游戏实现2

    微信小游戏开发教程-游戏实现2 绘制地面 类似于绘制背景,读者自行完成代码.src/runtime/land.js 简易View系统 坐标布局对于复杂的页面来说维护相当困难,因此这里我们引入布局的概念 ...

  8. 微信小游戏开发教程-游戏实现1

    微信小游戏开发教程-游戏实现1 概述 微信开发者工具官方提供一个飞机大战的游戏Demo,这里我们不再使用这个demo,我们以FlappyBird为例,为了让读者更加容易理解. 源码 https://g ...

  9. 微信小游戏开发教程-2D游戏原理讲解

    微信小游戏开发教程-2D游戏原理讲解 原理 为了更加形象的描述,这里先上一张图: 背景 a. 首先,我们看到背景好像是一张无限长的图片在向下移动.实际则不然,这是一张顶部和底部刚好重叠的图片.这是一种 ...

最新文章

  1. ccflow如何实现自由流程的?
  2. Android 中ContentProvider和Uri详解
  3. MySQL 命令行导出、导入Select 查询结果
  4. linux rm命令参数及用法详解---linux删除文件或目录命令
  5. NPOI “发现 中的部分内容有问题,是否要恢复此工作薄的内容?如果信任此工作薄的来源。。。”的问题的解决方法...
  6. 2017.9.18 calc 失败总结
  7. c++ max函数_「C/C++」函数:定义、调用、参数传递
  8. Django的virtualenv环境搭建
  9. MyEclipse6.5的SVN插件的安装
  10. 关于自定义函数的创建和调用
  11. 按键精灵修改html内容,按键精灵将表格录入网页.doc
  12. plsql连接不上64位oracle,PLSQL Developer 不能连接 64位 Oracle 11g 的解决办法
  13. 我们开发中常用的常用浏览器常用插件,比如FeHelper,React Developer Tools, Vue Devtools,沙拉查词,Infinity,OneTab,AdGuard等
  14. 不透明度百分比 16进制值对照表;rgb色值16进制转化原理
  15. 全桥逆变电路MOS管的关断尖峰怎么解决
  16. canvas自定义多边形
  17. 分享一个通过网络链接PDF转JPG的公用方法
  18. python画拓扑图权值是线条粗细_拓扑图线条流动效果
  19. enumerate使用方法
  20. 实现 企业微信认证 网络准入认证 配置

热门文章

  1. 推荐系统-03-简单基于用户的推荐
  2. [从菜鸟到高手演变]之智力题【史上最全】 (转)
  3. C#实现Windows服务的制作安装和删除
  4. 例解 autoconf 和 automake 生成 Makefile 文件
  5. Vista下的程序集缓存卸载方法,也就是C:\Windows\assembly之下的卸载方法
  6. [原创]软件测试思维方式
  7. 【DOM编程艺术】动态创建标记(签)---创建和插入节点
  8. 来一次有侧重点的区分Swift与Objective-C
  9. Maven配置JRE版本
  10. 台积电将开始量产 iPhone 8 A11 芯片 10纳米工艺