2016-08-18 105 views
-3

我正在嘗試創建一個最小的靜態HTML頁面,其中運行了voxel-hello-world。爲此,我創建了this project如何在頁面加載時運行此JavaScript腳本?

當我打開voxel1.html頁面時,我什麼都看不到。如果一切正常,我會在瀏覽器中看到類似Minecraft的景觀。

我假設的問題是,bundle.js腳本的以下部分不執行,當頁面正在加載:

module.exports = function(opts, setup) { 
    setup = setup || defaultSetup 
    var defaults = { 
    generate: voxel.generator['Valley'], 
    chunkDistance: 2, 
    materials: ['#fff', '#000'], 
    materialFlatColor: true, 
    worldOrigin: [0, 0, 0], 
    controls: { discreteFire: true } 
    } 
    opts = extend({}, defaults, opts || {}) 

    // setup the game and add some trees 
    var game = createGame(opts) 
    var container = opts.container || document.body 
    window.game = game // for debugging 
    game.appendTo(container) 
    if (game.notCapable()) return game 

    var createPlayer = player(game) 

    // create the player from a minecraft skin file and tell the 
    // game to use it as the main player 
    var avatar = createPlayer(opts.playerSkin || 'player.png') 
    avatar.possess() 
    avatar.yaw.position.set(2, 14, 4) 

    setup(game, avatar) 

    return game 
} 

什麼是確保上面這段代碼是正確的方式執行,當頁面加載?

注意:不要告訴我安裝Node.JS或任何其他JavaScript服務器。我想運行這個特殊的例子沒有服務器。

回答

1

您可以使用window.onload並通過您function或者你想進入的function

window.onload = function(){ 

} 
+0

對不起,意外點擊倒計時..恢復:) –

-1

假設所有其他的JS都已經到位了身體什麼,你有jQuery的,您只需:

var game; 
$(document).ready(function(){ 
game=module.exports(opts,setup); 
}) 
相關問題