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

第34節 - LibGDX: Keyboard, Mouse & Touch Screen Control (Polling Input)

這一節我會介紹LibGDX的Keyboard, Mouse & Touch Screen Control。

我在第33節 - LibGDX: Animation & Spritesheet介紹過圖片(hero1.png)向右移動的動畫效果,這一節會介紹用Keyboard、Mouse和Touch Screen控制。

要在程式內用Keyboard, Mouse 和 Touch Screen Control控制圖片移動,主要有以下兩種方法:

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

注意,這一節會介紹Polling Input的用法,以下會簡單介紹Polling Input和Event Driven Input的分別,至於Polling Input和Event Driven Input的詳細介紹,我會在第35節 - LibGDX: Input Control (Polling VS Event Driven Input)介紹。

Polling Input VS Event Driven Input

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

LibGDX: Keyboard, Mouse & Touch Screen Control (Polling 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: Keyboard, Mouse & Touch Screen Control (Polling 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動作才會執行。

製作Spritesheet

LibGDX: Keyboard, Mouse & Touch Screen Control (Polling Input)
  1. 我在第18節 - LibGDX: Texture Packer介紹過如何用Texture Packer軟件把不同的圖片(.png)合併成一張sprite sheet。以上用了六張簡單圖片做出Standing Left, Standing Right, Walking Left 和Walking Right動作。

製作動畫 - TextureRegion & Animation Class

LibGDX: Keyboard, Mouse & Touch Screen Control (Polling Input)
  1. 把Spritesheet(hero1Atlas.pack)傳入TextureAtlas物件(hero1Atlas)內。

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

  3. 建立Keyboard, Mouse或Touch Screen的程式碼:
    Keyboard: Gdx.input.isKeyPressed()
    Mouse: Gdx.input.isButtonPressed()
    Touch Screen: Gdx.input.isTouched()

  4. 把hero1Frame最新的格數傳入sprite內。

  5. 把sprite顯示在屏幕上。

Main Part

以下會詳細介紹主要的部分:

1) enum 型態

enum全名是Enumeration,Enum 型態是一組固定的常數 (constant) ,在以下例子裡,我們可以建立一個State(enum 型態),並設定Standing和Walking,這樣做我們就產生了:
State.Standing
State.Walking

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

LibGDX: Keyboard, Mouse & Touch Screen Control (Polling Input)

以下是動畫的四種可能性:
Standing Left
Standing Right
Walking Left
Walking Right

LibGDX: Keyboard, Mouse & Touch Screen Control (Polling Input)

2) Input Control

以下是Keyboard程式碼:

LibGDX: Keyboard, Mouse & Touch Screen Control (Polling Input)
  1. 用Gdx.input.isKeyPressed()方法判斷向左或向右,最後把最新的狀態傳入hero1Frame內。

以下是Mouse程式碼:

LibGDX: Keyboard, Mouse & Touch Screen Control (Polling Input)
  1. 用Gdx.input.isButtonPressed()方法判斷向左或向右,最後把最新的狀態傳入hero1Frame內。

以下是Touch Screen程式碼:

LibGDX: Keyboard, Mouse & Touch Screen Control (Polling Input)
  1. 用Gdx.input.isTouched()方法判斷向左或向右,最後把最新的狀態傳入hero1Frame內。

    注意,我把屏幕分為左右兩部分,如果X座標<0,相等於左面; 如果X座標>0,相等於右面。

例子1 - Keyboard Input (Polling Input)

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

DesktopLauncher.java

LibGDX: Keyboard, Mouse & Touch Screen Control (Polling Input)
  1. DesktopLauncher是PC Desktop的Starter Class,我們在DesktopLauncher內設定顯示的大小為1024 X 768 px。
LibGDX: Keyboard, Mouse & Touch Screen Control (Polling Input)
  1. 把hero1Atlas.pack和hero1Atlas.png儲存到Android的Assets文件夾內。

MyDemo34_1.java

LibGDX: Keyboard, Mouse & Touch Screen Control (Polling 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: Keyboard, Mouse & Touch Screen Control (Polling Input)

例子2 - Mouse Input (Polling Input)

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

MyDemo34_2.java

LibGDX: Keyboard, Mouse & Touch Screen Control (Polling 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: Keyboard, Mouse & Touch Screen Control (Polling Input)

例子3 - Touch Screen Input (Polling Input)

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

MyDemo34_3.java

LibGDX: Keyboard, Mouse & Touch Screen Control (Polling 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: Keyboard, Mouse & Touch Screen Control (Polling Input)

以下是例子3 - Touch Screen Input (Polling Input)程式執行的動畫:


Download above code and sample pictures here!