2014-11-23 89 views
3

我該如何讓Phaser遊戲自動填充瀏覽器窗口? (理想情況下,如果窗口更改大小,則自動填充。)我知道有一個ScaleManager class,但文檔不清楚如何使用它。我也發現了other instructions,但他們沒有指出在哪裏添加他們建議的代碼,並且在創建Game類後立即運行它似乎沒有任何作用。如何讓Phaser遊戲自動填充窗口?

我目前使用的是發現的基本代碼in this tutorial,但我願意完全改變它。

回答

3

結果很簡單,您只需要在預加載或創建函數中運行此代碼(而不是在創建遊戲實例後立即運行,因爲我一直在嘗試)。

this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL; 
//this.scale.pageAlignHorizontally = true; 
this.scale.pageAlignVertically = true; 
this.scale.setScreenSize(true); 

注意this.scale.pageAlignHorizo​​ntally設置爲true,實際上會弄亂水平對齊方式,如果你做遊戲去全屏。相反,你可以用CSS中心畫布:

canvas { margin: 0 auto; } 
+2

** this.scale.setScreenSize(真); **不移相器新版本的工作。那麼應該使用什麼? – 2015-09-02 07:09:06

+0

@ShohanurRahaman我不知道對不起,你應該爲新版本問一個新問題。 – curiousdannii 2015-09-02 07:11:51

1

可以覆蓋ScaleManager的setShowAll方法
默認情況下,該功能具有如果它的「擴大」開關,
擴張是被設置爲true的參數,如果你去到全屏模式。
但是因爲我們總是想「擴大」我只是刪除了if。

module.exports = function() { 

    Phaser.ScaleManager.prototype.setShowAll = function() { 
    let bounds = this.getParentBounds(this._tempBounds); 
    let width = bounds.width; 
    let height = bounds.height; 
    let multiplier = Math.max((height/this.game.height), (width/this.game.width)); 

    this.width = Math.round(this.game.width * multiplier); 
    this.height = Math.round(this.game.height * multiplier); 
    }; 

    window.Game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL; 
}; 

這個殺死更新的可能性,但它的工作原理