移除一些舊程式碼並實現遊戲介面
工具/原料
libgdx
Assembling the game world 組裝遊戲世界
我們將要移除我們用來繪製所有的檢測用的sprites的程式碼。另外我們會新增三個常量來標識初始生命值和GUI相機的視窗尺寸。
Change the code of Constantsclass as follows:
對Constant類的程式碼做出如下改變:程式碼部分請參照圖書
Now, remove these two lines of code in WorldController:
移除WorldController的這兩行程式碼:
***public Sprite[] testSprites;***
***public int selectedSprite;***
Additionally, removethe following methods in WorldController:
另外移除WorldController中的如下方法:
****• initTestObjects()****
****• updateTestObjects()****
****• moveSelectedSprite()****
移除WorldController的handleDebugInput()方法中的如下程式碼:程式碼... ...
Next, remove the code below the two comments "Select next sprite" and "Toggle
camera follow" in the keyUp()method of WorldControllerso that the resulting
method looks like this:
移除WorldController中keyUp()方法的"Select next sprite"和
"Toggle camera follow"兩個註釋的 內容以便程式碼如下所示:
把下面的兩個實現程式碼新增到WorldController
import com.packtpub.libgdx.canyonbunny.game.objects.Rock;
import com.packtpub.libgdx.canyonbunny.util.Constants;
下一步新增下面程式碼到WorldController:
public Level level;
public int lives;
public int score;
private void initLevel () {
score = 0;
level = new Level(Constants.LEVEL_01);
}
Change the code in the init()method of WorldController as follows:
如下面程式碼更改WorldController的init方法:。。。 。。。
最後移除update()中 對刪除了的TestObjects()方法的呼叫。
我們現在移除了所有的舊程式碼並添加了level 載入到controller中。
還有兩個變數score 和lives 來計算玩家的生命值和分數。
我們還必須對 CameraHelper類做一些小的改變,把對sprite 的使用切換到AbstractGameObject。
Remove the following import line in CameraHelper:
移除CameraHelper中的import程式碼:
***import com.badlogic.gdx.graphics.g2d.Sprite;
下一步新增程式碼到CameraHelper::
import com.packtpub.libgdx.canyonbunny.game.objects.
AbstractGameObject;
現在對CameraHelperas 做出如下改變:程式碼。。。 。。。 。。。
Next, add the following code in WorldRenderer:
向WorldRenderer中新增程式碼如下:
移除WorldRenderer中的renderTestObjects() 方法
最後用rederWorld()方法替換已經刪除的renderTestObjects()方法。
public void render () {
renderWorld(batch);
}
現在世界繪製器將會呼叫renderWorld()方法,它會呼叫Level 的render()方法,繪製所有的載入的level的遊戲物件。