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)
上一篇 2021年7月23日 下午4:00
下一篇 2021年7月23日 下午5:00


相关推荐

  • Java 介绍

    1 Java 介绍 Java的历史 Java的历史非常有趣。 Java最初是为交互式电视而设计的,但是对于当时的数字有线电视行业来说,它是太先进的技术。 Java的历史始于绿色团队…

    2022年1月24日
    51
  • 解决Windows10专业版无法安装语言包!!!

    解决Windows10专业版无法安装语言包!!!今天下午把我用了两年多的家庭版的电脑升级成了专业版 然后就想把中文改成英文系统试一试 但是现实很让我失望 我弄了一下午 各种论坛写吧博客几乎看遍了 也没能解决我的问题 就是语言安装不了 尝试点击设置 时间和语言 区域和语言 添加语言在面板里面添加语言之后就显示 可用的语言包 或者 正在搜索 Windows 更新 点 选项 进去之后 点击下载语言包之后进度条并没有走动

    2026年3月26日
    1
  • java handlersocket_HandlerSocket java客户端「建议收藏」

    java handlersocket_HandlerSocket java客户端「建议收藏」MySQL中有个handlersocket,直接跳过数据库处理中的sql分析过程,直接调用存储引擎的接口,可以大幅度提升数据库的性能。我的测试中,使用handlersocket与sql,可以提升约3-5倍,在我的环境中单条“增删改查”可以达到2.5TPS左右,没有作者说得可以达到10万TPS。有一个hs4j的java客户端,但是有些功能没有完善,还有一个不太正常的现象,就是连接时间长了,就会断开,…

    2022年8月24日
    7
  • Cursor IDE 使用指南:像带实习生一样带你的 AI 编程搭档

    Cursor IDE 使用指南:像带实习生一样带你的 AI 编程搭档

    2026年3月16日
    2
  • ubuntu9.10 软件推荐

    ubuntu9.10 软件推荐一 工具 1 输入法平台每个输入法平台都能加装很多种类输入法 如二笔 五笔 拼音 均为国产 ibus K Ubuntu9 10 预设输入法平台 为取代停滞的 scim 创造 fcitx 小企鹅输入法平台 scim K Ubuntu9 10 之前版本使用的 gtk 输入法平台 项目停止 ucimf 命令行模式中文输入法 一般配合 fbterm 命令行终端中文支持使用 Fireinput

    2026年3月18日
    1
  • Switch 语句

    Switch 语句

    2021年9月18日
    63

发表回复

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

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