游戏制作器 JS 扩展

Game Maker JS Extension

您好,我想为游戏制作者添加 javascript 功能,但它们的格式是这样的:

companyname.initialize({ 
    soundMuteCallback: muteSound,           // optional
    soundUnmuteCallback: unmuteSound        // optional
});

在文件中它们看起来像这样

this.initialize = function(params) {
    companyname.getSharedEventCenter().postEvent(SharedEventKeys.API_INITIALIZE);

    _isInitialized = true;

    if (typeof params !== "undefined") {
        var muteSoundCallback = ("soundMuteCallback" in params && typeof params["soundMuteCallback"] === "function") ? params["soundMuteCallback"] : undefined;
        var unmuteSoundCallback = ("soundUnmuteCallback" in params && typeof params["soundUnmuteCallback"] === "function") ? params["soundUnmuteCallback"] : undefined;
        _adsManager.setSoundCallbacks(function() {
            typeof muteSoundCallback === "function" && muteSoundCallback();
            [].forEach.call(document.getElementsByTagName("audio"), function(element){
                element.muted = true;
            });
        }, function() {
            typeof unmuteSoundCallback === "function" && unmuteSoundCallback();
            [].forEach.call(document.getElementsByTagName("audio"), function(element){
                element.muted = false;
            });
        });
    }

    _tryShowAd();
};

有谁知道如何在 game maker 中执行此操作?我不知道在扩展功能属性中要输入什么信息。

谢谢, 米切尔。

我建议创建一个 Game Maker 可以理解的新函数,然后使用它来创建您的对象和您在此处显示的构造函数。

company.initialize = function(params) {
    companyname.getSharedEventCenter().postEvent(SharedEventKeys.API_INITIALIZE);

    _isInitialized = true;

    if (typeof params !== "undefined") {
        var muteSoundCallback = ("soundMuteCallback" in params && typeof params["soundMuteCallback"] === "function") ? params["soundMuteCallback"] : undefined;
        var unmuteSoundCallback = ("soundUnmuteCallback" in params && typeof params["soundUnmuteCallback"] === "function") ? params["soundUnmuteCallback"] : undefined;
        _adsManager.setSoundCallbacks(function() {
            typeof muteSoundCallback === "function" && muteSoundCallback();
            [].forEach.call(document.getElementsByTagName("audio"), function(element){
                element.muted = true;
            });
        }, function() {
            typeof unmuteSoundCallback === "function" && unmuteSoundCallback();
            [].forEach.call(document.getElementsByTagName("audio"), function(element){
                element.muted = false;
            });
        });
    }

    _tryShowAd();
};

function createMuteCallback() {
    muteCallback = function () {
        // Code to handle the callback
    }
    return muteCallback;
}

function createUnmuteCallback() {
    unmuteCallback = function () {
        // Code to handle the callback
    }
    return unmuteCallback;
}

function createCompany (mute, unmute) {
    if (mute == 1) {
        soundMuteCallback.createMuteCallback();
    }

    if (unmute == 1) {
        soundUnmuteCallback.createUnmuteCallback();
    }

    company.initialize(soundMuteCallback, soundUnmuteCallback);
}

所以所有这些都在同一个 .js 文件中。在 Game Maker 中创建一个新的扩展。将 .js 文件添加到该扩展名。添加一个名为 createCompany 的函数,带有两个可选参数。

然后当你调用 createCompany(1, 1);在您的 Game Maker 代码中,.js 文件将 运行 并将使用两个回调函数初始化公司对象。

希望对您有所帮助。