Phaser3屏幕適配iPhoneX、iPhoneXs的坑 -- JavaScript Html5 游戲開發(fā)

PhaserJS

坑:
在config內(nèi)不要把 width 設(shè)為 window.innnerWidth
在config內(nèi)不要把 width 設(shè)為 window.innnerWidth
在config內(nèi)不要把 width 設(shè)為 window.innnerWidth

重要的事情得說三遍...

var game;
// once the window loads...
window.onload = function () {
    // 接收 websocket;
    // config of the game;
    var config = {
        type: Phaser.AUTO,
        parent: 'bitgame',
        width: 640, // don't window.innerWidth 
        height: 512,
        physics: {
            default: 'arcade',
            arcade: {
                gravity: {
                    y: 0
                },
                debug: false,
            }
        },
        //*** scenes used by the game
        scene:  [BootScene,PlayGameScene,UIScene]
    }
    game = new Phaser.Game(config);
    // game.scene.add('Boot', BootScene); //*** key,class */
    // game.scene.add('PlayGame', PlayGameScene);
    // game.scene.add('UI', UIScene);
    // game.scene.start('Boot');

    window.focus();
    resize();
    window.addEventListener('resize', resize, false);
}
 
function resize() {
      
    var canvas = document.querySelector('canvas');
    var windowWidth = window.innerWidth;
    var windowHeight = window.innerHeight;
    var windowRatio = windowWidth / windowHeight;
    var gameRatio =  game.config.width / game.config.height;
    if (windowRatio < gameRatio) {
        canvas.style.width = windowWidth + 'px';
        canvas.style.height = (windowWidth / gameRatio) + 'px';
    } else {
        canvas.style.width = (windowHeight * gameRatio) + 'px';
        canvas.style.height = windowHeight + 'px';
    }


}

更多游戲開源教學(xué):www.iFIERO.com -- 為游戲開發(fā)深感自豪

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容