Event handling

Table of Contents

a note from the translation

Wiki Style Guide

  • Developer's Guide
    • Introduction
    • Goals & Features
    • Community & Support
    • Contributing
    • Games Built with Libgdx
    • Prerequisites
    • Gradle Project Setup, Running, Debugging and Packaging
      • Dependency management with Gradle
    • Project Setup, Running & Debugging
      • Manual Project Setup
      • Maven Integration
      • Using libgdx with IntelliJ IDEA
      • Using libgdx with Android Studio
      • RoboVM Notes
      • GWT Notes
      • GWT Super Dev Mode
    • Third Party Services
      • AdMob in Libgdx
      • Swarm in Libgdx
      • NextPeer in Libgdx
      • Google Play Game Services in LibGDX
    • Working from Source
      • Running Demos & Tests
      • Building libgdx from Source
    • Using libgdx with other JVM languages
      • Using libgdx with Clojure
      • Using libgdx with Python
      • Using libgdx with Scala
    • The Application Framework
      • The Life-Cycle
      • Modules Overview
      • Starter Classes & Configuration
      • Querying
      • Logging
      • Threading
      • Interfacing with Platform-specific Code
    • A Simple Game
      • Extending the Simple Game
    • File Handling
    • Networking
    • Preferences
    • Input Handling
      • Configuration & Querying
      • Mouse, Touch & Keyboard
        • Polling
        • Event Handling
      • Controllers
      • Gesture Detection
      • Simple Text Input
      • Accelerometer
      • Compass
      • Vibrator
      • Cursor Visibility & Catching
      • Back and Menu Key Catching
      • On-screen Keyboard
    • Memory Management
    • Audio
      • Sound Effects
      • Streaming Music
      • Playing PCM Audio
      • Recording PCM Audio
    • Graphics
      • Configuration & Querying Graphics ??
      • Fullscreen & VSync
      • Continuous & Non-Continuous Rendering
      • Clearing the Screen
      • Take a Screenshot
      • OpenGL ES Support
        • Configuration & Querying OpenGL ??
        • Direct Access ??
        • Utility Classes
          • Rendering Shapes
          • Textures & TextureRegions
          • Meshes
          • Shaders
          • Frame Buffer Objects
      • 2D Graphics
        • SpriteBatch, TextureRegions, and Sprite
        • 2D Animation
        • Clipping, with the use of ScissorStack
        • Orthographic camera
        • Mapping Touch Coordinates ??
        • Viewports
        • NinePatches
        • Bitmap Fonts
          • Distance field fonts
        • Using TextureAtlases
        • Pixmaps
        • Packing Atlases Offline
        • Packing Atlases at Runtime
        • 2D Particle Effects
        • Tile Maps
        • scene2d
          • scene2d.ui
          • Skin
      • 3D Graphics
        • Quick Start
        • Models
        • Material and environment
        • 3D animations and skinning
        • Importing Blender models in LibGDX
        • Perspective Camera ??
        • Picking ??
    • Managing Your Assets
    • Utilities
      • Reading & Writing JSON
      • Reading & Writing XML
      • Collections
      • Reflection
      • jnigen
    • Math Utilities
      • Interpolation
      • Vectors, Matrices, Quaternions
      • Circles, Planes, Rays, etc.
      • Path interface & Splines
      • Bounding Volumes ??
      • Intersection & Overlap Testing ??
    • Physics
      • Box2D
      • Bullet Physics
    • Tools
      • Texture Packer
      • Hiero
      • Particle Editor
    • Extensions
      • gdx-audio
      • gdx-freetype
    • Deploying your Application
    • Building Libgdx ??
    • Known Issues
  • Articles
    • Getting Help
    • External Tutorials
    • Bundling a JRE
    • Saved Game Serialization
  • Deprecated (May be outdated)
    • Graphics Module
      • Screen & Viewport
    • Misc
      • Integrating Libgdx and the Device camera

Event handling allows you to get more granular and most of all chronological information about input from the user. Event handling provides a way to implement interactions with user interfaces, where specific input sequences are important, e.g. touch down, touch up on a button means the user clicked the button. Such interactions are hard to implement with polling.

Input Processor

Event handling is done using the commonobserver pattern. First we have to implement a listener interface called InputProcessor:

public class MyInputProcessor implements InputProcessor {   @Override   public boolean keyDown (int keycode) {      return false;   }   @Override   public boolean keyUp (int keycode) {      return false;   }   @Override   public boolean keyTyped (char character) {      return false;   }   @Override   public boolean touchDown (int x, int y, int pointer, int button) {      return false;   }   @Override   public boolean touchUp (int x, int y, int pointer, int button) {      return false;   }   @Override   public boolean touchDragged (int x, int y, int pointer) {      return false;   }   @Override   public boolean touchMoved (int x, int y) {      return false;   }   @Override   public boolean scrolled (int amount) {      return false;   }}

The first three methods allow you to listen for keyboard events:

  • keyDown(): Called when a key was pressed down. Reports the key code, as found inKeys.
  • keyUp(): Called when a key was lifted. Reports the key code as above.
  • keyTyped(): Called when a Unicode character was generated by the keyboard input. This can be used to implement text fields and similar user interface elements.

The next three methods report mouse/touch events:

  • touchDown(): Called when a finger went down on the screen or a mouse button was pressed. Reports the coordinates as well as the pointer index and mouse button (alwaysButtons.LEFTfor touch screens).
  • touchUp(): Called when a finger was lifted from the screen or a mouse button was released. Reports the last known coordinates as well as the pointer index and mouse button (alwaysButtons.Leftfor touch screens).
  • touchDragged(): Called when a finger is being dragged over the screen or the mouse is dragged while a button is pressed. Reports the coordinates and pointer index. The button is not reported as multiple buttons could be pressed while the mouse is being dragged. You can useGdx.input.isButtonPressed()to check for a specific button.
  • touchMoved(): Called when the mouse is moved over the screen without a mouse button being down. This event is only relevant on the desktop and will never occur on touch screen devices where you only gettouchDragged()events.
  • scrolled(): Called when the scroll wheel of the mouse was turned. Reports either -1 or 1 depending on the direction of spin. This will never be called for touch screen devices.

Each of the methods returns aboolean. We'll look into why that is in the InputMultiplexer section below.

Once you implement yourInputProcessoryou have to tell libgdx about it so it can be called when a new input event arrives:

MyInputProcessor inputProcessor = new MyInputProcessor();Gdx.input.setInputProcessor(inputProcessor);

From this point on, all new input events will be pushed to theMyInputProcessorinstance. Events are dispatched right before the call toApplicationListener.render(), on the rendering thread.

InputMultiplexer

Sometimes you want to chainInputProcessors, e.g. you have one processor for your UI which should be invoked first, and a second processor for input events that manipulate your game's world. You can use theInputMultiplexerclass to achieve this:

InputMultiplexer multiplexer = new InputMultiplexer();multiplexer.addProcessor(new MyUiInputProcessor());multiplexer.addProcessor(new MyGameInputProcessor());Gdx.input.setInputProcessor(multiplexer);

TheInputMultiplexerwill hand any new events to the firstInputProcessorthat was added to it. If that processor returns false from the method invoked to handle the event, this indicates the event was not handled and the multiplexer will hand the event to the next processor in the chain. Through this mechanism, theMyUiInputProcessorcan handle any events that fall inside one of its widgets and pass on any other events to theMyGameInputProcessor.

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android引路蜂地图开发示例:第一个地图应
  2. android手机图片查看
  3. Android(安卓)- Looper.prepare()和Loope
  4. runONUIThread 分析与使用
  5. Android NDK之JNI使用例子
  6. Android之android:theme设置在Applicatio
  7. 深入了解android平台的jni(二)
  8. Android弹出软键盘布局是否上移问题
  9. Android AlarmManager的取消
  10. Mediaplayer中通过create函数获取资源时P