0
我想知道如何產生一種方式來產生多個不同的精靈或矩形。它是使用數組嗎?現在下面的代碼產生了每秒產生一個隨機位置的同一個矩形(精靈)。所以我希望它自己隨機選擇不同的矩形(精靈)?(JavaGameMaking的新功能)Libgdx隨機產生矩形(精靈?)
public class Chibi implements ApplicationListener {
Texture fallL;
OrthographicCamera camera;
SpriteBatch batch;
Array<Rectangle> chibis1;
long lastDropTime;
@Override
public void create() {
//Load image
fallL = new Texture(Gdx.files.internal("fallL.png"));
camera = new OrthographicCamera();
camera.setToOrtho(false,800,400);
batch = new SpriteBatch();
chibis1 = new Array<Rectangle>();
spawnChibi();
}
private void spawnChibi() {
Rectangle chibi1 = new Rectangle();
chibi1.x = MathUtils.random(0, 800-64);
chibi1.y = 480;
chibi1.width = 64;
chibi1.height = 64;
chibis1.add(chibi1);
lastDropTime = TimeUtils.nanoTime();
}
@Override
public void dispose() {
fallL.dispose();
bgMusic.dispose();
points.dispose();
lvup.dispose();
batch.dispose();
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
for(Rectangle chibi1 : chibis1){
batch.draw(fallL, chibi1.x, chibi1.y);
}
batch.end();
if (TimeUtils.nanoTime() - lastDropTime >1000000000) spawnChibi();
//process user input
Iterator<Rectangle> iter = chibis1.iterator();
while(iter.hasNext()){
Rectangle chibi1 = iter.next();
chibi1.y -= 200*Gdx.graphics.getDeltaTime();
if(chibi1.y + 64 < -60) iter.remove();
}
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
}
也請記住,即時通訊新的這個Java遊戲製作和Java。 :)
爲真。那是我打算做的計劃。我會仔細看看的。 – NewbieProgrammer
好的。我會仔細看看的。謝謝。我打算創建一個由4個實例組成的實例(例如box,bomb,heart ...),它每秒隨機選擇一個實例在屏幕上產生,當用戶點擊它時,它將被銷燬 – NewbieProgrammer
聽起來不錯。祝你好運。 –