0

Flappy Bird のように地面を描くことができないので、私は非常にイライラしています... 私はこの方法を使用しようとしています:

private void drawGround(){  
    for(Rectangle mRectangleGroundHelper : mArrayGround){
        if(spawnGround & mRectangleGroundHelper.x<0){ // spawn Ground if the actual ground.x + ground.width() is smaller then the display width.
            mArrayGround.add(mRectangleGroundHelper);
            spawnGround = false;
        }
    }
    for(Rectangle mRectangleGroundHelper : mArrayGround){
        if(mRectangleGroundHelper.x < -mTextureGround.getWidth()){ // set boolean to true, if the actual ground.x completely hide from display, so a new ground can be spawn
            spawnGround = true;
        }
    }

    for(Rectangle mRectangleGroundHelper : mArrayGround){ // move the ground in negative x position and draw him...
        mRectangleGroundHelper.x-=2;
        mStage.getSpriteBatch().draw(mTextureGround, mRectangleGroundHelper.x, mRectangleGroundHelper.y);
    }
}

1 回の結果は、ground.x がディスプレイの左側に当たると、地面が負の x 方向に速く移動することです。では、私のメソッドのエラーは何ですか?

4

1 に答える 1

1
mRectangleGroundHelper.x-=2;

これは恐ろしいコードです。一般に、基本的に世界全体を動かす必要があるため、地面をまったく動かすべきではありません。

代わりに、実際には単なる X 変数である「ビューポート」位置を作成します。ゲームが進むにつれて、ビューポートを前方に移動し (実際には X++ のみ)、それに対してすべてのオブジェクトを描画します。

そうすれば、地面を「生成」する必要はまったくありません。あなたはそれを描くか、描かないかのどちらかです。

これは、データに関する多くの仮定に基づいた大まかな例です...

private void drawGround(){
    viewport.x += 2;
    for(Rectangle r : mArrayGround) {
        if(r.x + r.width > viewport.x && r.x < viewport.x + viewport.width) {
            mStage.getSpriteBatch().draw(mTextureGround, viewport.x - r.x, r.y);
        }
    }
}
于 2014-02-28T23:11:17.593 に答える