Major Softwares

  INDEX PAGE

  1. 簡介
  2. Java, Android和LibGDX好書推介
  3. Java, Android和LibGDX要學的知識
  4. 用Windows寫Java程式
  5. 用Eclipse寫Java程式
  6. 用Eclipse寫Android程式
  7. 用Eclipse寫LibGDX程式
  8. Standard Java Naming Conventions
  9. System.out.println()的用法和意思
  10. Where is main() method in Android?

LibGDX - Splash & Menu Screen

  1. LibGDX: World, Texture, Background, Camera, Viewport, Screen & OpenGL
  2. LibGDX: Texture, TextureRegion, SpriteBatch & Sprite
  3. UML(Unified Modeling Language): Class Diagram
  4. Type Casting, Upcasting & Downcasting
  5. @Override的用法和意思
  6. LibGDX: Scene2d & Graphical User Interface(GUI)
  7. LibGDX: Splash Screen
  8. LibGDX: Texture Packer
  9. LibGDX: BitmapFonts, JSON & Skin
  10. Android: R.java File
  11. Android: onClick事件的5種實現方式
  12. LibGDX: Game Menu Screen
  13. LibGDX: Advanced Game Menu Screen (using Abstract Screen)
  14. LibGDX: Disposable Interface
  15. Java & LibGDX: super keyword
  16. LibGDX: Advanced Game Menu Screen (using AssetManager)
  17. Java: Array, Arrays, List, ArrayList & LibGDX: Array
  18. LibGDX: File I/O (Preferences)
  19. LibGDX: Game Level Selection Screen
  20. LibGDX: Advanced Game Level Selection Screen (using AssetManager)

LibGDX - Tiled 2D Platform Game

  1. LibGDX: Lifecylce (Render() Method)
  2. LibGDX: Delta Time
  3. LibGDX: Animation & Spritesheet
  4. LibGDX: Keyboard, Mouse & Touch Screen Control
  5. Input Control (Polling VS Event Driven Input)
  6. LibGDX: Tiled (Background and Foreground)
  7. LibGDX: Jumping Action
  8. LibGDX: Tiled (Collision Detection)
  9. LibGDX: Tiled (Bullet Class)
  10. LibGDX: Audio (Sound & Music)
  11. LibGDX: Tiled (Scrollable Background with Camera & HUD)
  12. LibGDX: WorldController & WorldRenderer Class

LibGDX/Java - Card Game No.1 - Blackjack

  1. LibGDX: Install & Setup Android Studio IDE
  2. LibGDX: Use Android Studio to Run Java Hello World
  3. LibGDX: Use Android Studio to Run LibGDX Hello World
  4. Adobe Illustrator: Basic Components Part 1
  5. Adobe Illustrator: Basic Components Part 2
  6. Adobe Illustrator: BlackJack Table & Cards
  7. LibGDX: Blackjack Animation
  8. LibGDX: Interpolation
  9. Java: toString() Method
  10. Java: Blackjack Shuffle Methods
  11. LibGDX: Blackjack Shuffle Method
  12. Java: Blackjack Card Game

LibGDX - Others

  1. Making and Displaying App Icon
  2. LibGDX: Displaying Traditional and Simplified Chinese Characters
  3. LibGDX: Handling Different Screen Resolutions

Unity Game Engine & C#

  1. Visual Studio: C# Hello World
  2. Unity: C# Hello World
  3. Unity: Handling Different Screen Resolutions
  4. Unity: Life Cycle
  5. Unity: StartCoroutine, StopCoroutine, IEnumerator & Yield
  6. Unity: Splash Screen
  7. Unity: Fonts, Traditional and Simplified Chinese Characters
  8. Unity: GameObject, Class Object, new & Instantiate
  9. Unity: Start Screen with Glowing Animated Button
  10. Unity: C# Get & Set Modifier
  11. Unity: Delegates & Events
  12. Unity: File I/O, Read & Write Text File & PlayerPrefs
  13. Unity: Game Level Selection Screen
  14. Unity: Game Menu Screen & ScreenManager
  15. Unity: Encrypt and Decrypt Text File
  16. Unity: Options Menu Screen
  17. Unity: Convert Numbers Image to Custom Font

Unity - Card Game No.1 - Blackjack

  1. Unity: Blackjack Card Game - Part 1 (Full Game)
  2. Unity: Blackjack Card Game - Part 2
  3. Unity: Blackjack Card Game - Part 3
  4. Unity: Blackjack Card Game - Part 4
  5. Unity: Blackjack Card Game - Part 5
  6. Unity: Blackjack Card Game - Part 6
  7. Unity: Blackjack Card Game - Part 7

以下是預告-Coming soon!


Secret Weapon No.1

  1. Unity: Card Game No.2

Advanced Programming

  1. Unity: GPS Programming
  2. Unity: User Login System
  3. Unity: Augmented Reality (AR)

Secret Weapon No.2

  1. Unity: GPS & AR Application

第35節 - LibGDX: Input Control (Polling VS Event Driven Input)

這一節我會介紹LibGDX的Input Control (Polling VS Event Driven Input)。

我在第33節 - LibGDX: Animation & Spritesheet介紹過圖片(hero1.png)向右移動的動畫效果; 第34節 - LibGDX: Keyboard, Mouse & Touch Screen Controlt介紹過Keyboard, Mouse和Touch Screen的Polling Input, 這一節我會介紹Polling Input與Event Driven Input的分別。

要在程式內用到Keyboard, Mouse & Touch Screen控制,主要有以下兩種方法,我會詳細介紹:

  1. 方式1 - Polling Input。
  2. 方式2 - Event Driven Input。

Polling Input VS Event Driven Input

以下是Polling Input和Event Driven Input的比較:

LibGDX: Input Control (Polling VS Event Driven Input)
  1. 如果用Polling Input,偵測Keyboard, Mouse或Touch Screen動作就會在render()方法內完成。

  2. 如果用Event Driven Input,首先須要實作InputProcessor介面。

  3. 設定監聽動作Gdx.input.setInputProcessor(this);

  4. 注意,Keyword "this"相等於這個類別的物件。"this"=MyDemo34類別的物件=ApplicationAdapter類別物件=InputProcessor介面物件。因為MyDemo34類別繼承ApplicationAdapter,也實作了InputProcessor介面,所以"this"就可作MyDemo34或InputProcessor的物件用。

    我也在第17節 - LibGDX: Splash Screen介紹過Keyword "this"。

  5. 在這例子裡,當Keyboard, Mouse或Touch Screen按下按鈕時,就會啟動以下方法:
    keyDown()
    keyUp()
    keyTyped()
    touchDown()
    touchUp()
    touchDragged()
    mouseMoved()
    scrolled()

以下是Polling Input和Event Driven Input的比較圖表:

LibGDX: Input Control (Polling VS Event Driven Input)
  1. 在Polling Input裡,偵測Keyboard, Mouse或Touch Screen動作就會在render()方法內完成。

    在Event Driven Input裡,偵測Keyboard, Mouse或Touch Screen動作就會在新增方法內完成。

  2. 在Polling Input裡,我們須要用到Gdx.input.isKeyPressed(),Gdx.input.isButtonPressed()和Gdx.input.isTouched()。

    在Event Driven Input裡,偵測Keyboard, Mouse或Touch Screen動作就會在新增方法內完成。

  3. 在Polling Input裡,因為偵測Keyboard, Mouse或Touch Screen動作會在render()方法內完成,所以在60 FPS環境下,Gdx.input.isKeyPressed(),Gdx.input.isKeyPressed()或Gdx.input.isKeyPressed()一秒會執行60次。

    在Event Driven Input裡,只會偵測到Keyboard, Mouse或Touch Screen動作才會執行。

以下收錄一些英文的解釋:

Polling Input

It is more efficient to use Event Drive Input for keyboard, mouse and touch screen input, rather than Polling. If you are doing input control in game loop (i.e. within render() method), this is going to happen on every frame or interval (60FPS), and this is not efficient approach.

Event Driven Input

It is important to realize that a keyDown event will be fired for each individual key fired, so if you want to hadnle multiple simutanious key presses, this may not be the best way to approach the subject.

If you want to move an actor using the Input Processor, you will notice that it will move only when the keydown is pressed. To continuously handle input, or to move a sprite, you could add a "flag" to your actor.

注意,最後提及的"flag",就是在程式內偵測狀態,則Yes/No,true/false等等。

我們所用的enum type,則State.Standing和State.Walking,就是用來做程式的"flag"。

我在第34節 - LibGDX: Keyboard, Mouse & Touch Screen Controlt介紹過enum type:
State.Standing
State.Walking

它們就可用來判斷動畫的最新狀態。

例子1 - Keyboard Input (Polling Input)

注意,這個例子與第34節 - LibGDX: Keyboard, Mouse & Touch Screen Controlt的例子1相同。

MyDemo34_1.java

這個例子會詳細介紹用Keyboard控制動畫左右移動(Polling Input):

LibGDX: Input Control (Polling VS Event Driven Input)
  1. 建立State (enum type)。

  2. 把Spritesheet(hero1Atlas.pack)傳入TextureAtlas物件(hero1Atlas)內。

  3. 建立Standing Left, Standing Right, Animation Left和Animation Right動作。

  4. 用LibGDX的Gdx.graphics.getDeltaTime(),它可以敢得兩格(two frames)動畫之間的時間,用來控制動畫的速度。+=是用來聚積animationTime時間值。

    用animation.getKeyFrame()方法把animationTime變數傳入,這個方法可以自動計算animationTime的聚積時間值,並自動顯示正確的格數。

  5. 建立Keyboard的程式碼:
    Keyboard: Gdx.input.isKeyPressed()

  6. 把hero1Frame最新的格數傳入sprite內,並設定sprite的最新位置。

  7. 把sprite顯示在屏幕上。

  8. 因為我們有用到Viewport類別,當屏幕呎寸改變時,就須要更新Viewport。

  9. 最後呼叫dispose()方法,執行batch.dispose();和hero1Atlas.dispose();,釋放資源。

執行程式:

LibGDX: Input Control (Polling VS Event Driven Input)

例子2 - Mouse Input (Polling Input)

注意,這個例子與第34節 - LibGDX: Keyboard, Mouse & Touch Screen Controlt的例子2相同。

這個例子會詳細介紹用Mouse控制動畫左右移動(Polling Input):

MyDemo34_2.java

LibGDX: Input Control (Polling VS Event Driven Input)
  1. 建立State (enum type)。

  2. 把Spritesheet(hero1Atlas.pack)傳入TextureAtlas物件(hero1Atlas)內。

  3. 建立Standing Left, Standing Right, Animation Left和Animation Right動作。

  4. 用LibGDX的Gdx.graphics.getDeltaTime(),它可以敢得兩格(two frames)動畫之間的時間,用來控制動畫的速度。+=是用來聚積animationTime時間值。

    用animation.getKeyFrame()方法把animationTime變數傳入,這個方法可以自動計算animationTime的聚積時間值,並自動顯示正確的格數。

  5. 建立Mouse的程式碼:
    Mouse: Gdx.input.isButtonPressed()

  6. 把hero1Frame最新的格數傳入sprite內,並設定sprite的最新位置。

  7. 把sprite顯示在屏幕上。

  8. 因為我們有用到Viewport類別,當屏幕呎寸改變時,就須要更新Viewport。

  9. 最後呼叫dispose()方法,執行batch.dispose();和hero1Atlas.dispose();,釋放資源。

執行程式:

LibGDX: Input Control (Polling VS Event Driven Input)

例子3 - Touch screen Input (Polling Input)

注意,這個例子與第34節 - LibGDX: Keyboard, Mouse & Touch Screen Controlt的例子3相同。

這個例子會詳細介紹用Touch Screen控制動畫左右移動(Polling Input):

MyDemo34_3.java

LibGDX: Input Control (Polling VS Event Driven Input)
  1. 建立State (enum type)。

  2. 把Spritesheet(hero1Atlas.pack)傳入TextureAtlas物件(hero1Atlas)內。

  3. 建立Standing Left, Standing Right, Animation Left和Animation Right動作。

  4. 用LibGDX的Gdx.graphics.getDeltaTime(),它可以敢得兩格(two frames)動畫之間的時間,用來控制動畫的速度。+=是用來聚積animationTime時間值。

    用animation.getKeyFrame()方法把animationTime變數傳入,這個方法可以自動計算animationTime的聚積時間值,並自動顯示正確的格數。

  5. 建立Touch Screen的程式碼:
    Touch Screen: Gdx.input.isTouched()

  6. 把hero1Frame最新的格數傳入sprite內,並設定sprite的最新位置。

  7. 把sprite顯示在屏幕上。

  8. 因為我們有用到Viewport類別,當屏幕呎寸改變時,就須要更新Viewport。

  9. 最後呼叫dispose()方法,執行batch.dispose();和hero1Atlas.dispose();,釋放資源。

執行程式:

LibGDX: Input Control (Polling VS Event Driven Input)

例子4 - Keyboard, Mouse & Touch Screen Input (Event Driven Input)

這個例子會一次過把Keyboard、Mouse和Touch Screen程式碼放在以下例子來控制動畫左右移動(Event Driven Input):

LibGDX: Input Control (Polling VS Event Driven Input)
  1. 首先須要實作InputProcessor介面。。

  2. 建立State (enum type)。

  3. 把Spritesheet(hero1Atlas.pack)傳入TextureAtlas物件(hero1Atlas)內。

  4. 建立Standing Left, Standing Right, Animation Left和Animation Right動作。

  5. 設定監聽動作Gdx.input.setInputProcessor(this);

  6. 注意,Keyword "this"相等於這個類別的物件。"this"=MyDemo34類別的物件=ApplicationAdapter類別物件=InputProcessor介面物件。因為MyDemo34類別繼承ApplicationAdapter,也實作了InputProcessor介面,所以"this"就可作MyDemo34或InputProcessor的物件用。

    我也在第17節 - LibGDX: Splash Screen介紹過Keyword "this"。

  7. 用LibGDX的Gdx.graphics.getDeltaTime(),它可以敢得兩格(two frames)動畫之間的時間,用來控制動畫的速度。+=是用來聚積animationTime時間值。

    用animation.getKeyFrame()方法把animationTime變數傳入,這個方法可以自動計算animationTime的聚積時間值,並自動顯示正確的格數。

  8. 設定hero1Frame的最新格數。

  9. 把hero1Frame最新的格數傳入sprite內,並設定sprite的最新位置。

  10. 把sprite顯示在屏幕上。

  11. 因為我們有用到Viewport類別,當屏幕呎寸改變時,就須要更新Viewport。

  12. 最後呼叫dispose()方法,執行batch.dispose();和hero1Atlas.dispose();,釋放資源。

  13. 建立KeyDown()方法和KeyUp()方法的程式碼,這兩個方法就是供給Keyboard使用。

  14. 建立touchDown()方法和touchUp()方法的程式碼。

    注意,這兩個方法可以供給Mouse或Touch Screen使用。

執行程式:

LibGDX: Input Control (Polling VS Event Driven Input)

Download above code and sample pictures here!