2017-03-16 45 views
0

我想添加彈出到遊戲。我使用相位器,發現modal.js添加相位器彈出它似乎是有用的,但是當我試圖添加這個我得到一個錯誤「未捕獲TypeError:不能設置屬性'modal1'未定義」。我想我只是因爲我的編碼結構而得到這個錯誤。這裏是我的代碼向相位遊戲添加彈出

var reg={}; 
createModals: function(){ 
    reg.modal.createModal({ 
     type: "modal1", 
     includeBackground: true, 
     modalCloseOnInput: true, 
     itemsArr: [{ 
      type: "graphics", 
      graphicColor: "0xffffff", 
      graphicWidth: 300, 
      graphicHeight: 300, 
      graphicRadius: 40 
     }, { 
      type: "text", 
      content: "The white behind me\nis a [Phaser.Graphic]", 
      fontFamily: "Luckiest Guy", 
      fontSize: 22, 
      color: "0x1e1e1e", 
      offsetY: -50 
     }, ] 
    }); 

    }, 

    showModal1: function(){ 
    reg.modal.showModal("modal1"); 
    } 

可以與任何幫助......

回答

1

下面是一個完整的例子。

var reg = {}; 
 

 
function createModals() { 
 
    reg.modal.createModal({ 
 
      type:"modal1", 
 
      includeBackground: true, 
 
      modalCloseOnInput: true, 
 
      itemsArr: [ 
 
       { 
 
      type: "graphics", 
 
      graphicColor: "0xffffff", 
 
      graphicWidth: 300, 
 
      graphicHeight: 300, 
 
      graphicRadius: 40 
 
     }, { 
 
      type: "text", 
 
      content: "The white behind me\nis a [Phaser.Graphic]", 
 
      fontFamily: "Luckiest Guy", 
 
      fontSize: 22, 
 
      color: "0x1e1e1e", 
 
      offsetY: -50 
 
     } 
 
      ] 
 
     }); 
 
} 
 

 
function showModal1(){ 
 
    reg.modal.showModal("modal1"); 
 
} 
 

 
var GameState = function(game) { 
 
}; 
 

 
GameState.prototype.create = function() { 
 
    reg.modal = new gameModal(game); 
 
    createModals(); 
 
    var m1 = this.game.add.button(30, 50, "m1", showModal1); 
 
}; 
 

 
var game = new Phaser.Game(750, 380, Phaser.CANVAS, 'game'); 
 
game.state.add('game', GameState, true);
<script src="http://netgfx.com/trunk/games/phaser_modals/phaser.min.js"></script> 
 
<script src="http://netgfx.com/trunk/games/phaser_modals/modal.js"></script> 
 
<div style="font-family:'Luckiest Guy',cursive;visibility:hidden;opacity:0;position:fixed;">&nbsp;</div>

+0

我得到一個錯誤「遺漏的類型錯誤: 在patternsRatio.game1.create(game1.js:97) 在Phaser.StateManager:在新gameModal(8 modal.js)無法設置屬性 '模態' 的不確定 在Phaser.Loader.finishedLoading(phaser.js:74233)上的.loadComplete(phaser.js:30083) (在Phaser.Loader.processLoadQueue(phaser.js:74190) 的Phaser.Loader.asyncComplete(phaser.js:74263)上的 ) at Phaser.Loader.fileComplete(phaser.js:7512​​0) at HTMLImageElement.file.data.onload(phaser.js:74522)「 –

0

我這個LIB的創造者,似乎你是不是通過正確的遊戲對象的構造函數。

reg.modal = new gameModal(game); 

如果你的遊戲對象不叫game你需要傳遞正確的遊戲對象。

通常這來源於此行

var game = new Phaser.Game(1024, 768, Phaser.AUTO, 'game', null, true); 

game是一個全局變量

@Jatin帕蒂爾是他的回答是正確的。