ランダム・ウォーカー(円)他の軸よりもはるかに頻繁に上がります

ニコライAleksandrovsk:

だから、これは楽しみのためだけに、私が少しプロジェクトです。アイブ氏はlibgdxを使用してJavaでのランダム・ウォーカーを再現してみました。

それは(おそらく)正常に働いていると今、私は成功した私のコードはかなり考えます。

しかし、この一つの問題があり、円が他の軸よりも上方(Y軸+)はるかに頻繁に移動する傾向があります。

私は解決策を把握することが2日となっています。私が間違ってやったところ、まだ見つけることができません。

だからここのコードです

{
    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:

ここにあなたの問題があるようにRandom.nextInt(N)メソッドの上限は、排他的です

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

あなたが使用する必要がありますので、

    rand = r.nextInt(4);

あなたがやっていることは> 2 0-間のランドを生成している、あなたがダウンして行くために、Y軸が含まれるように、それは> 3 0-する必要があります

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=410478&siteId=1