【问题标题】:How to draw the Flappy Bird Ground?如何画出飞扬的鸟地?
【发布时间】:2014-02-28 22:43:14
【问题描述】:

我非常沮丧,因为我无法像 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 处移动得更快。那么我的方法有什么错误呢?

【问题讨论】:

  • 通常,您会使用诸如 androidengine 或 libgdx 之类的引擎来执行诸如 flappy bird 之类的事情。
  • 或者甚至在 Unity 之类的东西中构建它......
  • 我用 Libgdx 开发它...

标签: java android libgdx game-physics flappy-bird-clone


【解决方案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);
        }
    }
}

【讨论】:

  • 好吧,就我而言,视口是一个矩形,用户可以看到吗?所以我不移动视口,而是移动相机?曾经的问题是,我必须在游戏开始时创建对象?我用 LibGDX 开发
  • 在这种情况下,相机和视口是一回事。因此,如果您移动相机,则无需移动地面(或在现实世界中会移动的任何其他东西)。如果您没有很多对象,那么可以,在启动时创建它们。如果您有很多对象,则将您的数据分组到“区域”并一次加载整个区域。您必须找出允许您的程序加载而不会滞后的最佳区域大小(我猜想 zone1[x=0 到 10000]、zone2[x=10001 到 20000] 等。使用单独的线程会做到最好。
  • 给你一个大大的赞。这个答案太棒了!
  • 我有最后一个问题,移动相机的正确方法是 mStage.getCamera.translate(1, 0, 0);正确的?因为现在地面从右向左移动,但背景也在从右向左移动……?
  • 是的。这是移动相机的正确方法。如果您的背景不应该移动,那么您必须在每次绘制时将其设置为 x,或者将其硬编码为 0。您可以将其作为天空盒的一部分(如果您的库支持)。如果您使用的是 3d 库,您还可以使背景离相机更远(更多 z),它会移动得更慢。
猜你喜欢
  • 1970-01-01
  • 2021-08-16
  • 2020-11-18
  • 2022-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多