所以我用LibGDX製作了這個遊戲。我有一個GroundTop和Ground類,在屏幕上繪製地面和地面頂部(在Assets類中加載圖像),並最終應用與玩家的碰撞(這是一個平臺遊戲)。我試圖做的是繪製4個不同的Ground實例和2個不同的GroundTop實例,以進行測試。但是,每個只有一個。看起來每個實例都會覆蓋另一個實例,因爲只有顯示的實例具有在代碼中創建的最後一個實例的座標。爲什麼是這樣的,我怎樣才能讓每個實例都顯示出來?有什麼讓每個實例覆蓋類中的其他實例嗎?我完全失去了這一點。在此先感謝,並請詢問您有任何問題,幫助澄清我的問題,並幫助我找到一個解決方案:)爲什麼實例在LibGDX中相互覆蓋?
這裏是我的代碼:
Game.java
package com.mygdx.thetimetraveller;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.Array;
public class Game implements Screen {
public static int frameCount;
Array<Ground> groundList = new Array<Ground>();
Ground[] grounds = new Ground[1000];
GroundTop groundTop;
GroundTop groundTop2;
Game() {
load();
}
@Override
public void load() {
groundList.add(new Ground(50, 50));
groundList.add(new Ground(250, 250));
grounds[0] = new Ground(500, 500);
grounds[1] = new Ground(650, 650);
groundTop = new GroundTop(500, 20);
groundTop2 = new GroundTop(500, 200);
frameCount = 0;
}
@Override
public void display(SpriteBatch batch) {
frameCount ++;
groundList.get(0).Draw(batch);
groundList.get(1).Draw(batch);
grounds[0].Draw(batch);
grounds[1].Draw(batch);
groundTop.Draw(batch);
groundTop2.Draw(batch);
}
}
Ground.java
package com.mygdx.thetimetraveller;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class Ground {
private static double x;
private static double y;
Ground(double x, double y) {
this.x = x;
this.y = y;
}
public void Draw(SpriteBatch batch) {
batch.draw(Assets.simpleBlockTexture01_sprite, (float) x, (float) y);
}
}
GroundTop.java
package com.mygdx.thetimetraveller;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class GroundTop {
private static double x;
private static double y;
GroundTop(double x, double y) {
this.x = x;
this.y = y;
}
public void Draw(SpriteBatch batch) {
batch.draw(Assets.simpleBlockTextureTop01_sprite, (float) x, (float) y);
}
}
thetimetraveller,現在我對遊戲很好奇。 :) – Daniele
哈哈,那麼好吧。所以,長話短說,玩家找到一個時間機器,使用它,並不小心將一些飲料灑在它上面。然後他/她必須經過很多時區才能回家。我打算這樣做,以便用戶可以在特定時間選擇及時去哪裏。很高興看到你有興趣:D –
酷!祝你好運,我迫不及待地想玩! – Daniele