将Popup添加到移相器游戏中

时间:2017-03-16 12:25:35

标签: javascript jquery phaser-framework

我想在游戏中添加弹出窗口。我正在使用phaser并发现modal.js在phaser中添加弹出窗口它似乎很有用但是当我试图添加这个时我得到一个错误" Uncaught 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");
    }

对此有任何帮助......

2 个答案:

答案 0 :(得分:2)

这是一个完整的例子。

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>

答案 1 :(得分:1)

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

reg.modal = new gameModal(game);

如果您的游戏对象未被调用game,则需要传递正确的游戏对象。

通常这来自这一行

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

因此game是一个全局变量

@Jatin patil的回答是正确的。