- XNA 4 3D Game Development by Example:Beginner's Guide
- Kurt Jaegers
- 534字
- 2021-08-05 18:55:26
Time for action – customizing the Update() method
Add the following code snippet to the Update()
method before base.Update()
:
Vector2 moveDir = Vector2.Zero; KeyboardState keyState = Keyboard.GetState(); if (keyState.IsKeyDown(Keys.Up)) moveDir += new Vector2(0, -1); if (keyState.IsKeyDown(Keys.Down)) moveDir += new Vector2(0, 1); if (keyState.IsKeyDown(Keys.Left)) moveDir += new Vector2(-1, 0); if (keyState.IsKeyDown(Keys.Right)) moveDir += new Vector2(1, 0); if (moveDir != Vector2.Zero) { moveDir.Normalize(); moveDirection = moveDir; } playerPosition += (moveDirection * playerSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds); playerPosition = new Vector2( MathHelper.Clamp( playerPosition.X, 0, this.Window.ClientBounds.Width - 16), MathHelper.Clamp( playerPosition.Y, 0, this.Window.ClientBounds.Height - 16)); CheckCollisions(); CheckForNewWord();
What just happened?
During each frame, we will begin by assuming that the player is not pressing any movement keys. We create a Vector2
value called moveDir
and set it to the predefined value of Vector2.Zero
, meaning that both the x and y components of the vector will be zero.
In order to read the keyboard's input to determine if the player is pressing a key, we use the Keyboard.GetState()
method to capture a snapshot of the current state of all the keys on the keyboard. We store this in the keyState
variable, which we then use in a series of if
statements to determine if the up, down, left, or right arrow keys are pressed. If any of them are pressed, we modify the value of moveDir
by adding the appropriate vector component to its current value.
After all the four keys have been checked, we will check to see if the value is still Vector2.Zero
. If it is, we will skip updating the moveDirection
variable. If there is a non-zero value in moveDir
, however, we will use the Normalize()
method of the Vector2
class to divide the vector by its length, resulting in a vector pointing in the same direction with a length of one unit. We store this updated direction in the moveDirection
variable, which is maintained between frames.
When we have accounted for all of the possible inputs, we update the player's position by multiplying the moveDirection
by playerSpeed
and the amount of time that has elapsed since Update()
was last called. The result of this multiplication is added to the playerPosition
vector, resulting in the new position for the player.
Before we can assume that the new position is ok, we need to make sure that the player stays on the screen. We do this by using MathHelper.Clamp()
on both the X and Y components of the playerPosition
vector. Clamp()
allows us to specify a desired value and a range. If the value is outside the range, it will be changed to the upper or lower limit of the range, depending on which side of the range it is on. By limiting the range between zero and the size of the screen (minus the size of the player), we can ensure that the player's sprite never leaves the screen.
Finally, we call two functions that we have not yet implemented: CheckCollisions()
and CheckForNewWord()
. We discussed CheckForNewWord()
in the LoadContent()
section, but CheckCollisions()
is new. We will use this method to determine when the player collides with a letter and how to respond to that collision (increase or decrease the player's score, advance the spelling of the current word, and so on).
- 我們都是數據控:用大數據改變商業、生活和思維方式
- Word 2010中文版完全自學手冊
- 企業大數據系統構建實戰:技術、架構、實施與應用
- 文本挖掘:基于R語言的整潔工具
- Sybase數據庫在UNIX、Windows上的實施和管理
- 數據驅動設計:A/B測試提升用戶體驗
- OracleDBA實戰攻略:運維管理、診斷優化、高可用與最佳實踐
- Spark大數據分析實戰
- Oracle RAC日記
- SQL Server 2012數據庫管理教程
- Oracle高性能SQL引擎剖析:SQL優化與調優機制詳解
- Visual FoxPro數據庫技術基礎
- 數據庫查詢優化器的藝術:原理解析與SQL性能優化
- Python 3爬蟲、數據清洗與可視化實戰
- 中國云存儲發展報告