将 Popup 添加到 Phaser 游戏

Adding Popup to the phaser game

我想在游戏中添加弹窗。我正在使用移相器并发现 modal.js 在移相器中添加弹出窗口似乎很有用,但是当我尝试添加它时出现错误 "Uncaught TypeError: Cannot set property 'modal1' of undefined"。我想我只是因为我的编码结构而收到这个错误。这是我的代码

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");
    }

对此有什么帮助吗...

这是一个完整的例子。

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>

我是这个库的创建者,您似乎没有在构造函数上传递正确的游戏对象。

reg.modal = new gameModal(game);

如果你的游戏对象没有被调用game你需要传递正确的游戏对象。

通常来自这一行

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

所以game是一个全局变量

@Jatin patil 的回答是正确的。