简体   繁体   English

随机绘制瓷砖Libgdx Java

[英]Randomly drawing tiles Libgdx Java

Problem: 问题:

I am trying to create a game that is top down 2d and using a pixel tiled map style format, similar to a top down minecraft. 我正在尝试创建一个自上而下的2d游戏,并使用像素平铺的地图样式格式,类似于自上而下的minecraft。 The way i would like to do it is have a few different tiles, different shades of green and brown for grass. 我想做的方法是有一些不同的瓷砖,草的绿色和棕色的阴影也不同。 I would like to generate these 4 tiles randomly around the 1920*1080 pixel area to give a semi realistic effect. 我想在1920 * 1080像素区域周围随机生成这4个图块,以产生半逼真的效果。

Ideas: 思路:

Randomly select the tiles, assigning them a numerical value, picking a random number and using a case statement to select the relevant tile, then put them in order in an array (not sure how this would be done). 随机选择图块,为它们分配一个数值,选择一个随机数,并使用case语句选择相关图块,然后将它们按顺序排列在数组中(不确定如何做到)。 Then render them each using Tiled Map. 然后使用平铺地图渲染它们。 Any ideas??? 有任何想法吗???

Have tried this: 尝试过这个:

private void generateTile(){
    System.out.print("tiletry1");
    while(loadedTiles != 8100){

    System.out.print("tiletry");
    Texture currentTile = null;
    int tileX = 0;
    int tileY = 0;


    switch(MathUtils.random(3)){

    case 1:
        tileX+=16;
        tileY+=16;
        loadedTiles ++;
        //game.batch.draw(tile1, tileX, tileY);
        System.out.print("tile1");
        currentTile = tile1;
        break;
    case 2:
        tileX+=16;
        tileY+=16;
        loadedTiles ++;
        //game.batch.draw(tile2, tileX, tileY);
        System.out.print("tile2");
        currentTile = tile2;
        break;
    case 3:
        tileX+=16;
        tileY+=16;
        loadedTiles ++;
        //game.batch.draw(tile3, tileX, tileY);
        System.out.print("tile3");
        currentTile = tile3;
        break;
        }

    //game.batch.begin();
    //game.batch.draw(currentTile, tileX, tileY);
    //game.batch.end();
    }   

}

But all of the comments to do with rendering get errors for example removing these comments: 但是与渲染有关的所有注释都会出错,例如删除这些注释:

game.batch.begin();
        game.batch.draw(currentTile, tileX, tileY);
        game.batch.end();

Gives me this error: 给我这个错误:

tiletry1tiletrytile2tiletryException in thread "LWJGL Application" java.lang.NullPointerException
at com.badlogic.gdx.graphics.g2d.SpriteBatch.draw(SpriteBatch.java:495)
at com.MKgames.OptionScreen.generateTile(OptionScreen.java:130)
at com.MKgames.OptionScreen.<init>(OptionScreen.java:84)
at com.MKgames.game1.screen.playOptions.render(playOptions.java:76)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.MKgames.Game1.render(Game1.java:39)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)

MathUtils.random(3) may also return 0 , according to its documentation : 根据其文档MathUtils.random(3)也可能返回0

static int random(int range) 静态整数随机数(整数范围)
Returns a random number between 0 (inclusive) and the specified value (inclusive). 返回介于0(含)和指定值(含)之间的随机数。

So there is a 1-in-4 chance currentTile is accessed while it still is the null to which it is set at the start of the while-loop. 因此,当currentTile仍然是在while循环开始时设置为null时,有currentTile机会访问currentTile

Add a 添加一个

case 0:

to fix this. 解决这个问题。

A better way of doing this is to create an array of tile0 to tile3 and simply use the math random value to pick from this array; 更好的方法是创建一个tile0tile3的数组,然后简单地使用数学随机值从该数组中进行选择。 no need for the entire repeating case code. 不需要整个重复的case代码。


(Minor) You are increasing both x and y with (次要)您正在同时增加x和y

  tileX+=16;
  tileY+=16;

and so, when you get it working, you will get a diagonal line of grass tiles. 因此,当它开始工作时,您会得到一条对角线的草砖。 You should set x,y, to 0 at the start, then only increase x. 您应该在开始时将x,y设置为0,然后仅增加x。 When you filled an entire horizontal line, reset x to 0 and increase y. 当您填满整个水平线时,将x重置为0并增加y。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM