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);
    }
}

曾经的结果是,当 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 回答