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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • SPSS作业-卡方检验-列联表[通俗易懂]

    SPSS作业-卡方检验-列联表[通俗易懂]作业要求:某厂生产三种类型啤酒:淡啤酒、普通啤酒、黑啤酒。根据下列资料检验男性与女性饮酒者啤酒偏好是否相同。 作业数据: 淡啤 普啤 黑啤 合计 男性 20 40 20 80 女性 30

    2022年5月16日
    36
  • 修改mysql默认字符集的方法

    修改mysql默认字符集的方法

    2021年10月12日
    44
  • [Warning] large integer implicitly truncated to unsigned type [-Woverflow][通俗易懂]

    [Warning] large integer implicitly truncated to unsigned type [-Woverflow][通俗易懂][Warning]largeintegerimplicitlytruncatedtounsignedtype[-Woverflow]警告的原因是:整数溢出整数溢出:当整数达到它所能表述的最大值时,会重新从起点开始#include<stdio.h>intmain(void){ unsigneda=12345678910; printf(“a=%d\n”,a); return0;}该程序输出以后并不是输出a=12345678910而是:上面的代码

    2022年7月25日
    8
  • Java十大常用框架介绍

    Java十大常用框架介绍转载:https://www.toutiao.com/a6591434508417892871/?tt_from=copy_link&amp;utm_campaign=client_share&amp;timestamp=1535816836&amp;app=news_article&amp;utm_source=copy_link&amp;iid=42275378268&amp;utm_medi…

    2022年7月8日
    31
  • 联想笔记本电脑键盘灯怎么开启_联想键盘灯怎么开

    大家好,我是时间财富网智能客服时间君,上述问题将由我为大家进行解答。联想笔记本键盘灯的打开方法如下:1、正常情况下,在键盘上同时按下“FN”和“空格”键打开即可。2、部分早期的Thinkpad笔记本电脑需要通过“Fn+PageUp”组合键开启。如果电脑键盘的“Space(空格键)”按键上有类似太阳的标识符号,电脑一般带有键盘背光,使用“Fn+Space(空格键)”组合键即可开启键盘背光。若“Sp…

    2022年4月5日
    2.3K
  • eclipse中怎么自动补全_空格键坏了

    eclipse中怎么自动补全_空格键坏了eclipse自动补全及其空格键优化(去除空格自动补全)使用eclipse在创建其他工作区间的时候,想要配置代码自动补全,因为老是忘记,每次都要从网上查找,于是就自己总结一下。选1是代码自动补全,只需将“.”换为“.qwertyuiopasdfghjklzxcvbnm”就行了,看起来很乱,其实还是有规律可循的。(只需将键盘上的26字母按从左到右,从上到下的顺序按一遍就行了。)选2是空格不会自动补全,因为按空格会自动补全,所以有时候特别烦,而网上的大多数解决方法是需要改代码的,就会显得特别麻烦。于是

    2022年8月31日
    4

发表回复

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

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