【问题标题】:Adding Popup to the phaser game将弹出窗口添加到移相器游戏
【发布时间】:2017-08-07 15:02:19
【问题描述】:

我想在游戏中添加弹出窗口。我正在使用移相器并发现 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");
    }

有什么可以帮忙的吗...

【问题讨论】:

    标签: javascript jquery phaser-framework


    【解决方案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>

    【讨论】:

    • 我收到一个错误“Uncaught TypeError: Cannot set property 'modals' of undefined at new gameModal (modal.js:8) at patternsRatio.game1.create (game1.js:97) at Phaser .StateManager.loadComplete (phaser.js:30083) 在 Phaser.Loader.finishedLoading (phaser.js:74233) 在 Phaser.Loader.processLoadQueue (phaser.js:74190) 在 Phaser.Loader.asyncComplete (phaser.js:74263)在 Phaser.Loader.fileComplete (phaser.js:75120) 在 HTMLImageElement.file.data.onload (phaser.js:74522)"
    【解决方案2】:

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

    reg.modal = new gameModal(game);
    

    如果你的游戏对象不是game,你需要传递正确的游戏对象。

    通常来自这一行

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

    所以game 是一个全局变量

    @Jatin patil 的回答是正确的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-12
      • 2017-01-01
      • 1970-01-01
      • 2016-09-23
      • 2019-01-13
      • 2019-06-15
      • 2015-04-12
      • 1970-01-01
      相关资源
      最近更新 更多