XNA中的鼠标,键盘与操纵杆

XNA中的鼠标,键盘与操纵杆

                                
  XNA中的鼠标,键盘与操纵杆
                                                           
电子科技大学软件学院03级02班 周银辉



1, 鼠标
    对于鼠标有专门的Mouse类.   要检测鼠标的状态,可以通过Mouse类的静态函数GetMouseState()函数, 它将返回一个MouseState对象. 该对象保存了当前鼠标的状态信息. 比如其LeftButton属性指示当前鼠标左键是否被按下或释放.其它鼠标属性值同理.

    而执行鼠标检测的代码您应该在Game的 Update(GameTime gameTime)方法中执行. 除此之外,Game是默认隐藏鼠标的,为了显示鼠标您应该将其IsMouseVisible属性设置为true.

    这是一段示例代码:

   

ExpandedBlockStart.gif
ContractedBlock.gif
/**/
/// <summary>
InBlock.gif        
/// Allows the game to run logic such as updating the world,
InBlock.gif        
/// checking for collisions, gathering input and playing audio.
InBlock.gif        
/// </summary>
ExpandedBlockEnd.gif        
/// <param name=”gameTime”>Provides a snapshot of timing values.</param>



None.gif
        
protected
 
override
 
void
 Update(GameTime gameTime)
ExpandedBlockStart.gifContractedBlock.gif        

dot.gif
{

InBlock.gif            
// Allows the default game to exit on Xbox 360 and Windows
InBlock.gif
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
InBlock.gif                
this.Exit();
InBlock.gif
InBlock.gif         
InBlock.gif
InBlock.gif            
//mouse
InBlock.gif
            MouseState mouseState = Mouse.GetState();
InBlock.gif            
if (mouseState.LeftButton == ButtonState.Pressed)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{

InBlock.gif               
//
ExpandedSubBlockEnd.gif
            }

InBlock.gif
InBlock.gif            
base.Update(gameTime);
InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif        }

2 键盘
   与鼠标类似, 键盘有一个专门的Keyboard类, 要检测键盘的状态请使用Keyboard类的静态方法GetKeyboardState(),它将返回一个KeyboardState对象,该对象保存了当前的键盘状态. 要检测某个键是否被按下或释放可以使用其IsKeyDown(
Keys
key)或IsKeyUp(
Keys
key)方法. 除此之外您还可以使用其GetPressedKeys()方法获取当前被同时按下的几个键.

   与鼠标类似,检测也应该在Update(GameTime gameTime)方法中执行.

    这是一段示例代码:

ExpandedBlockStart.gif
ContractedBlock.gif
 
/**/
/// <summary>
InBlock.gif        
/// Allows the game to run logic such as updating the world,
InBlock.gif        
/// checking for collisions, gathering input and playing audio.
InBlock.gif        
/// </summary>
ExpandedBlockEnd.gif        
/// <param name=”gameTime”>Provides a snapshot of timing values.</param>



None.gif
        
protected
 
override
 
void
 Update(GameTime gameTime)
ExpandedBlockStart.gifContractedBlock.gif        

dot.gif
{

InBlock.gif            
// Allows the default game to exit on Xbox 360 and Windows
InBlock.gif
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
InBlock.gif                
this.Exit();
InBlock.gif
InBlock.gif
InBlock.gif            
//key
InBlock.gif
            KeyboardState keyState = Keyboard.GetState();
InBlock.gif            Keys[] keys 
= keyState.GetPressedKeys();
InBlock.gif            
foreach (Keys key in keys)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{

InBlock.gif                
//
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
InBlock.gif           
InBlock.gif            
base.Update(gameTime);
InBlock.gif
ExpandedBlockEnd.gif        }


None.gif

3 操纵杆
   其有一个专门的类叫做GamePad,检测其状态请使用GetState()方法. 没玩过Xbox360,其操纵杆的参数还不太熟悉. 但至少有一点是值得注意的,操纵杆要进行速度测试,其被保存在GamePadState对象的ThumbSticks属性中.其它的基本与鼠标和键盘类似

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/111120.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 火眼金睛审核 一键轻松处理[通俗易懂]

    火眼金睛审核 一键轻松处理——学籍助手简介学籍助手是专为河北省义务教育学籍管理系统区县端开发的一款辅助工具(图)。该工具不影响原系统正常使用,只是扩充了一些实用功能,只是让学籍管理变得异常轻松简单(如您不是区县学籍管理员,请勿下载)。学籍助手具有严格的入库审核、强大的批量处理和轻松的数据对接功能,犹如为原学籍管理系统赋予一双慧眼、插上一对翅膀,从容处理繁杂的学籍变…

    2022年4月13日
    50
  • typescript web_typescript在线运行

    typescript web_typescript在线运行前言上一篇我们讲到如何手动编译TypeScript,如果我们每次都要手动编译,那么这样会很累,接下来就介绍自动编译TypeScript。mac自动编译typescript在Typescript文

    2022年7月31日
    22
  • OSPF协议总结(1)

    OSPF协议总结(1)

    2021年4月14日
    182
  • pytest报错_pytest失败重跑

    pytest报错_pytest失败重跑前言我们每天写完自动化用例后都会提交到git仓库,随着用例的增多,为了保证仓库代码的干净,当有用例新增的时候,我们希望只运行新增的未提交git仓库的用例。pytest-picked插件可以

    2022年7月29日
    7
  • PyQt5入门教程[通俗易懂]

    PyQt5入门教程[通俗易懂]0x00安装环境清单我使用的环境如下:Windows10(Build17763)Python3.7.2VSCode1.33.0PyQt5QtDesigner如果你使用的是O

    2022年8月2日
    9
  • 下载Android SDK tools完成Android SDK 安装、配置环境变量

    下载Android SDK tools完成Android SDK 安装、配置环境变量大家都知道安卓是基于java开发的,安卓因为其开放包容的特性被广大的程序员所青睐。早期,开发Android用到IDE是开源的eclipse,同时Android为eclipse提供了开发用的插件,这促使了安卓的发展。随着Android的过于开放,导致Android吃硬件性能越来越厉害。现在,为了解决性能上面的缺陷,Google慢慢的开始收紧了对Android的控制。于是Google研发了自家的AndroidStudio。随着时间的发展,AndroidStudio越来越成熟。

    2022年7月21日
    19

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号