CocosCreator小游戏如何在pc端测试开发

版权声明:本文为博主原创文章,转载注明来源。 https://blog.csdn.net/u013654125/article/details/81559486

现在我们希望小游戏能在pc端做测试,但是这样会调用不了微信的一些接口。

所以,小游戏在pc端测试明显有两个问题:

1、如果在pc端就调用不到微信小游戏的一些接口;

2、如果每次测试都要打包成微信小游戏,这样又很麻烦。

很明显,我们不希望写完一句代码,想要看效果的时候,还得打包一次项目。

为此,我们需要做一些钩子(大家可以理解为匹配机制),为在pc端还是小游戏端来进行适配。

(钩子在js中和在java等强语言中是不一样的机制,在强语言中是利用接口来实现,大家有兴趣可以检阅资料)

我们来新建一个Hook.js文件:

第一种写法:

//下面的test一般为公司名
window.test = window.test || {};
window.test.Ctrl = window.test.Ctrl || {};
(function() {
    var Hook = {};
    
    Hook.showModal = function(title, content) {
        //PlatformUtil在这里就不写出来了
        if (!PlatformUtil.IsWeichatGame()) {
            Util.AssertNotice(tittle, content);
        } else {
            wx.showModal({
                title: title,
                content: content,
                showCancel: false,
                success: function (res) {
                    console.log("弹出窗成功!");
                }
            });
        }
    }

    window.test.Ctrl.Hook = Hook;
})();

第二种写法:

window.wx = wx || {}
wx.showModal = wx.showModal || function(title, content) {
    //在这里写自己的界面提示
}

猜你喜欢

转载自blog.csdn.net/u013654125/article/details/81559486