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属性中.其它的基本与鼠标和键盘类似

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值