游戏JavaScript / JSON链接文本框

Gaming JavaScript / JSON linking text boxes

本文关键字:文本 链接 JSON JavaScript 游戏      更新时间:2023-09-26

我正在制作一款游戏,并遇到了一些问题。现在我有一个文本框显示,说碰见我,继续下一关。当我遇到它时,下一关加载得很好,这部分工作得很好。现在我试图改变它一点,文本框显示,并有两个链接到两个不同的水平。游戏中有道具,我想做的是让玩家在拥有道具时进入一个关卡,在没有道具时进入另一个关卡。我的做法是给道具一个值,然后在与文本框发生冲突时,检查该值是否在库存中。所以主要的问题是,有可能将两个节点链接到一个文本框吗?如果是这样,我该怎么做呢?下面是我的一些代码:

从我的.js文件:

function Choice(scene, text, image, width, height, z) {
// Static textbox object that holds the text of a choice
tChoice = new TextBox(scene, image, width, height);
tChoice.text = text;
tChoice.choiceNumber = 0;
tChoice.z = z;
return tChoice;
} // end choice

从我的。html文件:

function checkChoiceCollisions() {
// If a player collides with a choice, that choice's level will be displayed.
for (var i = 0; i < choices.length; i ++) {
    if(player.collidesWith(choices[i])) {
        player.visible = false;
        switchNode(i);
    } // end if
} // end for
} // end checkChoiceCollisions
JSON:

"options":[{"text":"Run into me to continue'nto the next level","link":"nodes/nextLevel.txt","width":"150", "height":"75", "x":"800", "y":"250"}]

谢谢你的帮助!

编辑:我忘记添加这部分代码了。
function makeChoices() {
    // Makes the choice textboxes defined by the level.
        for (var i = 0; i < choices.length; i ++) {
        // Destroy the previous level's choice boxes.
            var index = spriteList.list.indexOf(choices[i]);
            spriteList.list.splice(index, 1);
        } // end for
        choices = [];
        for (var i = 0; i < curNode.options.length; i ++) {
        // Create each choice box defined by the level.
            var width = parseInt(curNode.options[i].width);
            var height = parseInt(curNode.options[i].height);
            var x = parseInt(curNode.options[i].x);
            var y = parseInt(curNode.options[i].y);
            var choice = new Choice(scene, curNode.options[i].text, null, width, height, 2);
            choice.setPosition(x, y);
            choice.fitText();
            choices.push(choice);
            if (!checkChoiceRequirements(i)) {
            // Only display a choice if its requirements are met.
                choices[i].visible = false;
            } // end if
        } // end for
    } // end makeChoices

这是一个问题,因为我缺乏评论点…

因为你的函数"Choice"是大写的,我希望它是一个伪经典构造函数,然后默认返回其隐式的"this"对象。但相反,你返回了'tChoice',这可能会产生意想不到的后果。

你还定义了一个'tChoice'对象的值,而没有通过'var'关键字声明它。您打算访问一个名为"tChoice"的全局变量吗?

否则,我不确定你到底在问"是否有可能将两个节点链接到一个文本框?"访问对象似乎是一种很好的方法。你得到一个错误吗?你可以张贴你的代码到小提琴/Plunkr/JS_Bin?