0

レベルの無限の背景を手伝っていただけませんか?

私は現在、slick2dを使用して原始的なゲームを書いています。ゲームプレイは、マリオに似ています。

私は2つの写真を持っています-img1とimg2(両方とも1280x480、画面解像度は640x480です)。最初、img2のXは==(img1のX + img1の幅)です。つまり、img1の最後に接着されています。img1が画面の左側の境界から外れると、そのX座標はimg2X+imgWidthになります。

ロジックは私には正しいように見えますが、時々写真がオーバーストライクされます(多くの場合、画面の約1/4)。ロジックに間違いはありますか?アプローチは良いですか?たぶん、そうするためのより簡単で正しい方法がありますか?

擬似コードは次のようになります。

class BkgDrawer {


Image img1 = new Image("imgs/background/bkg1.png");
Image img2 = new Image("imgs/background/bkg2.png");

int img1Width = img1.getWidth(); //1280
int img2Width = img2.getWidth(); //1280
int screenResolution = game.getResolution; //640

Vector2f position1 = new Vector2f (0,0);
Vector2f position2 = new Vector2f (position1.x+img1.getWidth(), 0); //initially position2 is glued to the end of img1

public void render(    ) {
  if (position1.x + img1Width < 0) { //the img is over the left border of the screen
      position1.x = position2.x + img2Width; //glue it to the end of  img2
  }
//the same for the img2
  if (position2.x + img2Width < 0) { //the img is over the left border of the screen
      position2.x = position1.x + img2Width; //glue it to the end of  img2
  }
  img1.draw(position1.x, position1.y);
  img2.draw(position2.x, position2.y);
  //move coordinate to get the background moving.
  position1.x -= MOVING_STEP;
  position2.x -= MOVING_STEP;
  }
}

たくさんのテキストと感謝を申し訳ありません

4

1 に答える 1

0

バグを1つだけ見つけましたが、2つの画像の幅が異なる場合にのみ影響します。2つのifステートメントが同じ幅を使用しているimg2Width

各背景のレンダリングと再配置を処理するための重複したコードがあることに気付いたかもしれません。背景コードをBackgroundクラスにリファクタリングすることをお勧めします。このクラスには、背景画像、位置、およびMOVING_STEPによって再配置するupdateメソッドが含まれています。私が上で述べたような間違いを避けるでしょう。

于 2012-07-21T22:24:08.983 に答える