2012-06-18 28 views
0

你能幫助這個關卡的無盡背景嗎?無盡的背景爲水平

我目前正在使用slick2d編寫一個原始遊戲,遊戲類似於Mario。

我有兩張圖片 - 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; 
    } 
} 

對不起,大量的文字和感謝

+0

這隻發生在一些時間? –

回答

0

我只發現了一個錯誤,它會只有當兩個圖像有不同的影響寬度:您的兩條if語句使用相同的寬度img2Width

您可能已經注意到您有重複的代碼來處理每個背景的渲染和重新定位。我可能會建議你將後臺代碼重構成一個Background類,該類包含背景圖像,位置和更新方法,通過MOVING_STEP重新定位它。你會避免像上面提到的那樣的錯誤。

+0

感謝您的提示。這只是我編寫的一個簡化的代碼,用於顯示算法,而不用複雜的代碼打擾社區。 –