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


相关推荐

  • CloudSim仿真流程研究(二)[通俗易懂]

    CloudSim仿真流程研究(二)[通俗易懂]org.cloudbus.cloudsim.examples.power.random里的例子IqrMc:publicclassIqrMc{ /** *Themainmethod. * *@paramargsthearguments *@throwsIOExceptionSignalsthatanI/Oexceptionhasocc…

    2022年10月13日
    4
  • 反射getmethod参数_java通过反射获取属性值

    反射getmethod参数_java通过反射获取属性值1、forName方法forName是一个静态方法,其作用:通过调用来获取类名对应的Class对象,同时将Class对象加载进来。如果将类名保存在字符串(如xml)中,就可以在程序运行时,动态调用加载。注意:只有调用的参数是类名或者方法时,才可用。2、newInstance()方法作用:将对象实例化。返回类型为Object。与new的区别在于,new可以带参,而newInstance()不可以,…

    2025年12月2日
    7
  • hdu 4691 最长的共同前缀 后缀数组 +lcp+rmq

    hdu 4691 最长的共同前缀 后缀数组 +lcp+rmq

    2022年1月5日
    66
  • linux系统查看网卡是否开启,查看Linux下网卡状态或 是否连接

    linux系统查看网卡是否开启,查看Linux下网卡状态或 是否连接1)通过mii-tool指令[root@localhostroot]#mii-tooleth0:negotiated100baseTx-FD,linkoketh1:nolink或[root@localhostroot]#mii-tool-veth0:negotiated100baseTx-FD,linkokproductinfo:vendor00:50:43,…

    2022年10月18日
    7
  • 通过CLOVER 引导Windows与Linux双系统

    #什么是CLOVER可能有些朋友对CLOVER还不了解,因为有很多大神已经做过介绍,我这就做个引用吧。黑苹果引导工具Clover配置详解(转自[三个表哥])#为什么是CLOVER引导?自从折腾过黑苹果后,深深被四叶草的个性化界面以及灵活设置给吸引了。然而并不是长期会有使用黑苹果的需求,以及公司的电脑只用Win10+Ubuntu双系统,虽然是正常使用,但,界面不好看呀!!作为一…

    2022年4月7日
    71
  • pandas中的loc和iloc_pandas获取指定数据的行和列

    pandas中的loc和iloc_pandas获取指定数据的行和列实际操作中我们经常需要寻找数据的某行或者某列,这里介绍我在使用Pandas时用到的两种方法:iloc和loc。目录1.loc方法(1)读取第二行的值(2)读取第二列的值(3)同时读取某行某列(4)读取DataFrame的某个区域(5)根据条件读取(6)也可以进行切片操作2.iloc方法(1)读取第二行的值(2)读取第二行的值(3)同时读取某行某列(4)进行切片操作loc:通过行、列的名称或标签来索引iloc:通过行、列的索引位置来寻找数据..

    2022年8月30日
    7

发表回复

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

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