手機遊戲多採用觸控來操作遊戲內容,我們要處理這些觸控輸入有兩個方式,一個是用低階的方式,只抓取觸控點,其餘的判斷油程式來處理,另一個是高階的方式,我們抓取使用者的操作手勢,再來進行後續處理。
手機遊戲多採用觸控來操作遊戲內容,我們要處理這些觸控輸入有兩個方式,一個是用低階的方式,只抓取觸控點,其餘的判斷油程式來處理,另一個是高階的方式,我們抓取使用者的操作手勢,再來進行後續處理。
Windows Phone提供的低階觸控總共有三種狀態 Press、Move、Release
我們來實作下面情況,小球會跟著手指觸控點移動。
在Game1宣告變數,包括小球、小球位置、小球移動速度。
Public Class Game1
Inherits Microsoft.Xna.Framework.Game
Private WithEvents graphics As GraphicsDeviceManager
Private WithEvents spriteBatch As SpriteBatch
Dim ball As Texture2D
Dim position As Vector2 = New Vector2(0, 240)
Dim velocity As Vector2 = New Vector2(0, 0)
End Class
LoadContent()載入小球
Protected Overrides Sub LoadContent()
' Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = New SpriteBatch(GraphicsDevice)
' TODO: use Me.Content to load your game content here
ball = Content.Load(Of Texture2D)("ball")
End Sub
在Update()裡,我們產生一個觸控集合touchState,當此集合不等於0時表示有觸控發生,此集合存放的是TouchLocation結構,從touchLocation.Position可以得到觸控點的座標,再來計算小球跟觸控點的位置差,用vector2.Normalize()將位置差正規化,可以得到小球移動的方向,乘上5後便是小球的速度。
Protected Overrides Sub Update(ByVal gameTime As GameTime)
' Allows the game to exit
If GamePad.GetState(PlayerIndex.One).Buttons.Back = ButtonState.Pressed Then
Me.Exit()
End If
' TODO: Add your update logic here
Dim touchState As TouchCollection
touchState = TouchPanel.GetState
If touchState.Count > 0 Then
Dim touchLocation As TouchLocation
touchLocation = touchState(0)
Dim dPosition As Vector2
dPosition = touchLocation.Position - position
velocity = Vector2.Normalize(dPosition) * 5
End If
position += velocity
MyBase.Update(gameTime)
End Sub
Draw()繪製小球
Protected Overrides Sub Draw(ByVal gameTime As GameTime)
GraphicsDevice.Clear(Color.CornflowerBlue)
' TODO: Add your drawing code here
spriteBatch.Begin()
spriteBatch.Draw(ball, position, Color.White)
spriteBatch.End()
MyBase.Draw(gameTime)
End Sub
程式執行結果如下 (範例下載)
Windows Phone提供了10種的手勢狀態判斷。
我們用前個範例改成Flick手勢來移動小球
Game1裡宣告變數,包括小球、位置、速度。
Public Class Game1
Inherits Microsoft.Xna.Framework.Game
Private WithEvents graphics As GraphicsDeviceManager
Private WithEvents spriteBatch As SpriteBatch
Dim ball As Texture2D
Dim position As Vector2 = New Vector2(0, 240)
Dim velocity As Vector2 = New Vector2(0, 0)
End Class
Initialize()啟用Flick手勢監聽 (如果要監聽兩種手勢寫法 GestureType.Tap or GestureType.Flick)。
Protected Overrides Sub Initialize()
' TODO: Add your initialization logic here
TouchPanel.EnabledGestures = GestureType.Flick
MyBase.Initialize()
End Sub
LoadContent()載入小球
Protected Overrides Sub LoadContent()
' Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = New SpriteBatch(GraphicsDevice)
' TODO: use Me.Content to load your game content here
ball = Content.Load(Of Texture2D)("ball")
End Sub
在Update()裡,While迴圈取得觸控螢幕的所有手勢,我們用TouchPanel.ReadGesture讀出手勢資料存入gesture裡,從gesture.Deta得到移動量,用vector2.Normalize()將移動量正規化,可以得到小球移動的方向,乘上5後便是小球的速度。
Protected Overrides Sub Update(ByVal gameTime As GameTime)
' Allows the game to exit
If GamePad.GetState(PlayerIndex.One).Buttons.Back = ButtonState.Pressed Then
Me.Exit()
End If
While (TouchPanel.IsGestureAvailable)
Dim gesture As GestureSample
gesture = TouchPanel.ReadGesture
If gesture.GestureType = GestureType.Flick Then
velocity = Vector2.Normalize(gesture.Delta) * 5
End If
End While
position += velocity
MyBase.Update(gameTime)
End Sub
Draw()繪製小球
Protected Overrides Sub Draw(ByVal gameTime As GameTime)
GraphicsDevice.Clear(Color.CornflowerBlue)
' TODO: Add your drawing code here
spriteBatch.Begin()
spriteBatch.Draw(ball, position, Color.White)
spriteBatch.End()
MyBase.Draw(gameTime)
End Sub
程式執行結果如下 (範例下載)