zoukankan      html  css  js  c++  java
  • XNA中的鼠标,键盘与操纵杆

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

    1, 鼠标
        对于鼠标有专门的Mouse类.   要检测鼠标的状态,可以通过Mouse类的静态函数GetMouseState()函数, 它将返回一个MouseState对象. 该对象保存了当前鼠标的状态信息. 比如其LeftButton属性指示当前鼠标左键是否被按下或释放.其它鼠标属性值同理.
        而执行鼠标检测的代码您应该在Game的 Update(GameTime gameTime)方法中执行. 除此之外,Game是默认隐藏鼠标的,为了显示鼠标您应该将其IsMouseVisible属性设置为true.
        这是一段示例代码:
       
    /// <summary>
            
    /// Allows the game to run logic such as updating the world,
            
    /// checking for collisions, gathering input and playing audio.
            
    /// </summary>
            
    /// <param name="gameTime">Provides a snapshot of timing values.</param>

            protected override void Update(GameTime gameTime)
            
    {
                
    // Allows the default game to exit on Xbox 360 and Windows
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                    
    this.Exit();

             

                
    //mouse
                MouseState mouseState = Mouse.GetState();
                
    if (mouseState.LeftButton == ButtonState.Pressed)
                
    {
                   
    //
                }


                
    base.Update(gameTime);


            }


    2 键盘
       与鼠标类似, 键盘有一个专门的Keyboard类, 要检测键盘的状态请使用Keyboard类的静态方法GetKeyboardState(),它将返回一个KeyboardState对象,该对象保存了当前的键盘状态. 要检测某个键是否被按下或释放可以使用其IsKeyDown(Keys key)或IsKeyUp(Keys key)方法. 除此之外您还可以使用其GetPressedKeys()方法获取当前被同时按下的几个键.
       与鼠标类似,检测也应该在Update(GameTime gameTime)方法中执行.
        这是一段示例代码:
     /// <summary>
            
    /// Allows the game to run logic such as updating the world,
            
    /// checking for collisions, gathering input and playing audio.
            
    /// </summary>
            
    /// <param name="gameTime">Provides a snapshot of timing values.</param>

            protected override void Update(GameTime gameTime)
            
    {
                
    // Allows the default game to exit on Xbox 360 and Windows
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                    
    this.Exit();


                
    //key
                KeyboardState keyState = Keyboard.GetState();
                Keys[] keys 
    = keyState.GetPressedKeys();
                
    foreach (Keys key in keys)
                
    {
                    
    //
                }

                
               
                
    base.Update(gameTime);

            }


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

  • 相关阅读:
    Python装饰器学习(九步入门)
    jQuery练习题
    JavaScript 练习题
    Apache的安装与下载
    非常好用的CSS样式重置表
    表单练习
    shell命令lsof
    IndentationError: unindent does not match any outer indentation level
    Zabbix监控mysql主从复制状态
    Zabbix监控php-fpm status
  • 原文地址:https://www.cnblogs.com/zhouyinhui/p/589136.html
Copyright © 2011-2022 走看看