我目前正在尝试制作一个涉及随机生成的世界的游戏。我目前有一个工作的启动画面和一个工作的perlin噪音,它正常化。我只需要一种实现代码来显示图像的方法。
代码:
public void show() {
batch = new SpriteBatch();
World world = new World();
int type = 0;
for (int y = 1; y < world.getlengthy();y++){
for (int x = 1; x < world.getlengthx();x++){
type = world.getvalue(x, y);
switch (type) {
case 1:sprite = new Sprite(tex1,0,0,16,16);sprite.setPosition(x, y);batch.begin();sprite.draw(batch);batch.end();
Gdx.app.log("", "x: " + x + " y: " + y);
break;
case 2:batch.begin();batch.draw(tex2, x, y);batch.end();
break;
case 3:batch.begin();batch.draw(tex3, x, y);batch.end();
break;
case 4:batch.begin();batch.draw(tex4, x, y);batch.end();
break;
case 5:batch.begin();batch.draw(tex5, x, y);batch.end();
break;
case 6:batch.begin();batch.draw(tex6, x, y);batch.end();
break;
case 7:batch.begin();batch.draw(tex7, x, y);batch.end();
break;
case 8:batch.begin();batch.draw(tex8, x, y);batch.end();
break;
}
}
答案 0 :(得分:1)
您的代码必须位于render
方法内。如果第一次显示屏幕,则会调用该节目。 (1次调用!)所以将for循环置于渲染周期内,它应该可以工作。
就像一个小小的提示。不要在渲染周期sprite = new Sprite(tex1,0,0,16,16);
内创建对象。始终初始化show方法中或构造函数内部的所有对象以保存rendertime。(它确实在帧速率上有很大的不同)
循环后只有begin()
一次和end()
。
例如像这样的东西。我仍然不会在渲染中创建精灵,但我不知道你的其余逻辑。希望这可以帮助!此致
@Override
public void show() {
batch = new SpriteBatch();
World world = new World();
}
@Override
public void render(float delta){
int type = 0;
batch.begin();
for (int y = 1; y < world.getlengthy();y++){
for (int x = 1; x < world.getlengthx();x++){
type = world.getvalue(x, y);
switch (type) {
case 1:
sprite = new Sprite(tex1,0,0,16,16);
sprite.setPosition(x, y);
sprite.draw(batch);
Gdx.app.log("", "x: " + x + " y: " + y);
break;
case 2:
batch.draw(tex2, x, y);
break;
case 3:
batch.draw(tex3, x, y);
break;
case 4:
batch.draw(tex4, x, y);
break;
case 5:
batch.draw(tex5, x, y);
break;
case 6:
batch.draw(tex6, x, y);
break;
case 7:
batch.draw(tex7, x, y);
break;
case 8:
batch.draw(tex8, x, y);
break;
}
}
batch.end();
}