XNA中的鼠标,键盘与操纵杆
来源:互联网 发布:白领学化妆 知乎 编辑:程序博客网 时间:2024/06/06 02:46
XNA中的鼠标,键盘与操纵杆
电子科技大学软件学院03级02班 周银辉
1, 鼠标
对于鼠标有专门的Mouse类. 要检测鼠标的状态,可以通过Mouse类的静态函数GetMouseState()函数, 它将返回一个MouseState对象. 该对象保存了当前鼠标的状态信息. 比如其LeftButton属性指示当前鼠标左键是否被按下或释放.其它鼠标属性值同理.
而执行鼠标检测的代码您应该在Game的 Update(GameTime gameTime)方法中执行. 除此之外,Game是默认隐藏鼠标的,为了显示鼠标您应该将其IsMouseVisible属性设置为true.
这是一段示例代码:
2 键盘
与鼠标类似, 键盘有一个专门的Keyboard类, 要检测键盘的状态请使用Keyboard类的静态方法GetKeyboardState(),它将返回一个KeyboardState对象,该对象保存了当前的键盘状态. 要检测某个键是否被按下或释放可以使用其IsKeyDown(Keys key)或IsKeyUp(Keys key)方法. 除此之外您还可以使用其GetPressedKeys()方法获取当前被同时按下的几个键.
与鼠标类似,检测也应该在Update(GameTime gameTime)方法中执行.
这是一段示例代码:
文章来源于 http://www.cnblogs.com/zhouyinhui 版权归原作者所有
电子科技大学软件学院03级02班 周银辉
1, 鼠标
对于鼠标有专门的Mouse类. 要检测鼠标的状态,可以通过Mouse类的静态函数GetMouseState()函数, 它将返回一个MouseState对象. 该对象保存了当前鼠标的状态信息. 比如其LeftButton属性指示当前鼠标左键是否被按下或释放.其它鼠标属性值同理.
而执行鼠标检测的代码您应该在Game的 Update(GameTime gameTime)方法中执行. 除此之外,Game是默认隐藏鼠标的,为了显示鼠标您应该将其IsMouseVisible属性设置为true.
这是一段示例代码:
/**//// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the default game to exit on Xbox 360 and Windows
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
//mouse
MouseState mouseState = Mouse.GetState();
if (mouseState.LeftButton == ButtonState.Pressed)
{
//
}
base.Update(gameTime);
}
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the default game to exit on Xbox 360 and Windows
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
//mouse
MouseState mouseState = Mouse.GetState();
if (mouseState.LeftButton == ButtonState.Pressed)
{
//
}
base.Update(gameTime);
}
2 键盘
与鼠标类似, 键盘有一个专门的Keyboard类, 要检测键盘的状态请使用Keyboard类的静态方法GetKeyboardState(),它将返回一个KeyboardState对象,该对象保存了当前的键盘状态. 要检测某个键是否被按下或释放可以使用其IsKeyDown(Keys key)或IsKeyUp(Keys key)方法. 除此之外您还可以使用其GetPressedKeys()方法获取当前被同时按下的几个键.
与鼠标类似,检测也应该在Update(GameTime gameTime)方法中执行.
这是一段示例代码:
/**//// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the default game to exit on Xbox 360 and Windows
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
//key
KeyboardState keyState = Keyboard.GetState();
Keys[] keys = keyState.GetPressedKeys();
foreach (Keys key in keys)
{
//
}
base.Update(gameTime);
}
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the default game to exit on Xbox 360 and Windows
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
//key
KeyboardState keyState = Keyboard.GetState();
Keys[] keys = keyState.GetPressedKeys();
foreach (Keys key in keys)
{
//
}
base.Update(gameTime);
}
3 操纵杆
其有一个专门的类叫做GamePad,检测其状态请使用GetState()方法. 没玩过Xbox360,其操纵杆的参数还不太熟悉. 但至少有一点是值得注意的,操纵杆要进行速度测试,其被保存在GamePadState对象的ThumbSticks属性中.其它的基本与鼠标和键盘类似
文章来源于 http://www.cnblogs.com/zhouyinhui 版权归原作者所有
- XNA中的鼠标,键盘与操纵杆
- XNA中的鼠标,键盘与操纵杆
- Windows程序中的键盘与鼠标
- 键盘与鼠标钩子
- vc6.0中的鼠标消息与键盘消息详解
- 用游戏操纵杆摸拟鼠标
- XNA中的拾取与碰撞检测
- XNA中的拾取与碰撞检
- 鼠标消息与键盘消息
- win32编程键盘与鼠标
- 鼠标消息与键盘消息
- 3.osg中的CallBack回调、NodeVisitor与鼠标键盘的交互
- Java中的鼠标事件和键盘事件
- Java中的字符串鼠标和键盘事件
- 《Java中的鼠标和键盘事件 》
- AngularJs 中的键盘事件和鼠标事件
- JS中的事件:鼠标、键盘、HTML事件
- vc鼠标消息与键盘消息
- [转]面向对象设计原则
- 像苹果工具条一样平滑连续地缩放
- 关于使用TestDriven.Net 2.0的一点补充
- 好玩! 一句代码,打造一个词霸
- XNA Game Studio Express 1.0正式版 发布
- XNA中的鼠标,键盘与操纵杆
- [转]关于安装配置XNA开发环境的详细说明
- 新手老问题---------跨线程的控件访问
- 绘制,不再闪烁
- 重新包装的钩子
- 字符的点阵显示(模拟户外广告显示屏)
- 软件易容术-----换肤
- 如何打造RSS阅读器
- 真正的代码宝库:Google Code Search