2014-01-25 52 views
2

我是相當新的libgdx和Java,但我正在學習一切,我可以!我正在開發一個正交2D平鋪遊戲。基本上,我有我的角色四處走動,鏡頭跟着他。我正在設置它,以便在角色靠近地圖邊緣時停止相機,以免看到黑色空間。相機應該停在邊緣,玩家應該繼續移動。LibGDX - 正交相機不會停在地圖邊緣

這是我移動相機的代碼。現在我一直在嘗試一切,現在有點亂。地圖是30x30。

if (mainPlayer.getPosition().x >= 15 && mainPlayer.getPosition().x <= 30) { 
    camera.position.x = mainPlayer.getPosition().x;  
} 

camera.position.y = mainPlayer.getPosition().y; 

camera.update(); 
camera.apply(gl); 

這是我的渲染方法。我只與x部分混淆,所以現在忽略y。

編輯我想我需要改寫那個。我知道如何讓它停止..它使用我的代碼,但我無法弄清楚如何確定在哪裏停止它。我在上面的代碼中使用了15,這是地圖的一半。當然這不起作用。

回答

2

編輯我想我需要改寫那個。我知道如何讓它停止..它 工程使用我的代碼,但我不知道如何確定在哪裏停止它。我在上面的代碼中使用了15,這是地圖的一半。其中 當然不起作用。

最小X將需要是:

map.position.x + camera.viewportWidth/2; 

和最大X將需要是:

map.position.x+mapwidth-camera.viewportWidth/2; 

假設地圖位置在(0, 0)你可以忽略map.position.x部分。

0

您需要將相機矩形的邊界修復到世界矩形內。像下面這樣的功能將有助於:

public void fixBounds() { 
    float scaledViewportWidthHalfExtent = viewportWidth * zoom * 0.5f; 
    float scaledViewportHeightHalfExtent = viewportHeight * zoom * 0.5f; 

    // Horizontal 
    if (position.x < scaledViewportWidthHalfExtent) 
     position.x = scaledViewportWidthHalfExtent; 
    else if (position.x > xmax - scaledViewportWidthHalfExtent) 
     position.x = xmax - scaledViewportWidthHalfExtent; 

    // Vertical 
    if (position.y < scaledViewportHeightHalfExtent) 
     position.y = scaledViewportHeightHalfExtent; 
    else if (position.y > ymax - scaledViewportHeightHalfExtent) 
     position.y = ymax - scaledViewportHeightHalfExtent; 
}