gpt4 book ai didi

javascript - TypeError : utils.getPlayerName is not a function

转载 作者:行者123 更新时间:2023-12-03 08:50:47 27 4
gpt4 key购买 nike

我对module.exports一定做错了。不幸的是,我似乎无法弄清楚。我正在作为控制台应用程序在Node.js中开发Yugioh游戏(是那个大 child 的纸牌游戏)(至少现在是这样)。任何帮助将不胜感激。

game.js:

var utils = require("./utils");
var legend = require("./legend");
var manager = require("./eventManager");

module.exports = {
data: {
players: {
0: {
name: null,
lifePoints: 4000
},

1: {
name: null,
lifePoints: 4000
}
},

phase: null,

turnOwner: null,

turnCount: 1,

hasEnded: false
},

draw: function(playerId, amount) {
utils.out("Draw " + amount + " -> " + utils.getPlayerName(playerId));
},

cyclePhase: function(phaseId) {
if (phaseId) {
// TODO
}
else {
if (this.data.phase == null) {
this.data.phase = legend.phases.standby;
this.cyclePhase();
}
else {
switch (this.data.phase) {
case legend.phases.standby: {
manager.triggerEvent(legend.phaseEvents.start_standby, this.data.turnOwner);
utils.out("Phase -> Stand-By Phase");

break;
}

case legend.phases.draw: {
manager.triggerEvent(legend.phaseEvents.end_standby, this.data.turnOwner);
manager.triggerEvent(legend.phaseEvents.in_draw, this.data.turnOwner);
utils.out("Phase -> Draw Phase");

break;
}

case legend.phases.main1: {
manager.triggerEvent(legend.phaseEvents.start_main1, this.data.turnOwner);
utils.out("Phase -> Main Phase 1");

break;
}

case legend.phases.battle: {
manager.triggerEvent(legend.phaseEvents.end_main1, this.data.turnOwner);
manager.triggerEvent(legend.phaseEvents.start_battle, this.data.turnOwner);
utils.out("Phase -> Battle Phase");

break;
}

case legend.phases.main2: {
manager.triggerEvent(legend.phaseEvents.end_battle, this.data.turnOwner);
manager.triggerEvent(legend.phaseEvents.start_main2, this.data.turnOwner);
utils.out("Phase -> Main Phase 2");

break;
}

case legend.phases.end: {
manager.triggerEvent(legend.phaseEvents.end_main2, this.data.turnOwner);
manager.triggerEvent(legend.phaseEvents.in_end, this.data.turnOwner);
utils.out("Phase -> End Phase")

break;
}
}
}
}

this.data.turnCount++;

manager.triggerEvent(legend.turnEvents.countUpdate, null, this.data.turnCount);
},

activateCard: function(playerId, cardId, activationType, slotId) {

},

summonMonster: function(cardId, summonType) {
// TODO
},

setCard: function(cardType, slotId, slotPos, handSlotPos) {
switch (cardType) {
case 0: {

}
}
}
};

utils.js:
var game = require("./game");

module.exports = {
out: function(msg) {
console.log(" " + msg);
},

getPlayerName: function(playerId) {
return game.data.players[playerId].name;
},

getRandomInt: function(min, max) {
return min + Math.floor(Math.random() * (max - min + 1));
}
};

堆栈跟踪:
TypeError: utils.getPlayerName is not a function
at Object.module.exports.draw (/home/ubuntu/workspace/game.js:29:53)
at start (/home/ubuntu/workspace/engine.js:16:10)
at Object.<anonymous> (/home/ubuntu/workspace/engine.js:6:1)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:974:3

最佳答案

我自己弄清楚了。我不明白,但我设法解决了该错误。我从文件“utils.js”中删除了变量“game”,并将函数“getPlayerName”移到了文件“game.js”中,从而以某种方式解决了该问题。

关于javascript - TypeError : utils.getPlayerName is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41819655/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com