Random Walker (círculo) ascender mucho más frecuente que otro eje

Nikolai Alexandrovsk:

Así que este es un pequeño proyecto por mí, sólo por diversión. He intentado recrear Walker al azar en Java usando libgdx.

Ahora considero mi código más o menos éxito, ya que está funcionando correctamente (tal vez).

Pero no es este un problema, el círculo tiende a moverse hacia arriba (eje Y +) mucho más frecuente que otro eje.

Ha sido 2 días para mí averiguar la solución. Todavía no puede encontrar donde hice mal.

Así que aquí está el código

{
    ShapeRenderer sr;
    OrthographicCamera cam;
    Random r;
    int rand;
    float x;
    float y;

    @Override
    public void create()
    {
        sr = new ShapeRenderer();
        cam = new OrthographicCamera();
        cam.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

        r = new Random();

        x = Gdx.graphics.getWidth()/2;
        y = Gdx.graphics.getHeight()/2;
    }

    @Override
    public void render()
    {        
        cam.update();   

        rand = r.nextInt(3);



        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        sr.begin(ShapeRenderer.ShapeType.Filled);
        sr.setColor(Color.RED);
        sr.circle(x, y, 10);
        sr.end();

        switch(rand)
        {
            case 0:
                x = x + 100 * Gdx.graphics.getDeltaTime();
                break;

            case 1:
                x = x - 100 * Gdx.graphics.getDeltaTime();
                break;
            case 2:
                y = y + 100 * Gdx.graphics.getDeltaTime();
                break;

            case 3:
                y = y - 100 * Gdx.graphics.getDeltaTime();
                break;
                default:

        }
    }```
JoeChris:

Por lo que su problema aquí es los límites superiores en un método Random.nextInt (n) es exclusiva

    // Returns number between 0-9 
    int next = ran.nextInt(10); 

Así que hay que utilizar

    rand = r.nextInt(4);

Lo que estás haciendo está generando rand entre 0-> 2, se necesita que sea 0-> 3 para incluir el eje Y para bajar

Supongo que te gusta

Origin http://10.200.1.11:23101/article/api/json?id=410481&siteId=1
Recomendado
Clasificación