所以我一直在試圖把一個簡單的基於文本的簡單RPG放在一起,我剛剛完成了爲按鈕和不同類型的字符創建對象。當我停下來休息一天並保存時,我沒有任何錯誤,但是我今天又回來了,並收到錯誤「Can not read property'x'of undefined。」該錯誤沒有列出任何代碼行,並且除此之外我沒有列出任何錯誤。我只有在按鈕對象使用「X」,所以我認爲這個問題是在這個代碼塊的地方:無法讀取屬性'x'的undefined
//selection buttons
var Button = function(config) {
this.x = config.x || 5;
this.y = config.y || 2;
this.width = config.width || 390;
this.height = config.height || 96;
this.vrtx = config.vrtx || 50; //curve on the corners
this.lable = config.lable || "Click Here";
this.r = config.r || 100;
this.b = config.b || 100;
this.g = config.g || 100;
};
Button.prototype.draw = function() {
strokeWeight(5);
stroke(this.r/2, this.b/2, this.g/2);
fill(this.r, this.b, this.g);
rect(this.x, this.y, this.width, this.height, this.vrtx);
fill(0, 0, 0);
textSize(27);
textAlign(LEFT, TOP);
text(this.label, this.x + 10, this.y + this.height/4);
};
var btn1 = new Button(/*customize button*/);
只是櫃面它不是在這一節中,在這裏,我的代碼的其餘部分:
//classes
var mage;
var warrior;
var rogue;
var cleric;
//stats
var manaLvl;
var strengthLvl;
var stealthLvl;
var xpLvl = 10;
var enemyCount = 0; //# of enemies defeted
var lvl; //player's level
//player stats and level up
var character = function(health, armour, packSize, packItems, mana, strength, stealth, xp) {
this.health = health;
this.defArmour = armour;
this.packSize = packSize;
this.packItems = packItems;
this.mana = mana;
this.strength = strength;
this.stealth = stealth;
this.xp = xp;
};
Character.prototype.lvlUp = function() {
this.health += 2;
this.packSize += 1;
this.mana += manaLvl;
this.strength += strengthLvl;
this.stealth += stealthLvl;
enemyCount = 0;
};
character.prototype.xpGain = function() {
this.xp = enemyCount * xpLvl;
if (this.xp === 100) {
character.lvlUp();
}
};
character.prototype.packStorage = function() {
if (this.packItems < this.packSize) {
//allow player to store items
}
};
//selection buttons
var Button = function(config) {
this.x = config.x || 5;
this.y = config.y || 2;
this.width = config.width || 390;
this.height = config.height || 96;
this.vrtx = config.vrtx || 50; //curve on the corners
this.lable = config.lable || "Click Here";
this.r = config.r || 100;
this.b = config.b || 100;
this.g = config.g || 100;
};
Button.prototype.draw = function() {
strokeWeight(5);
stroke(this.r/2, this.b/2, this.g/2);
fill(this.r, this.b, this.g);
rect(this.x, this.y, this.width, this.height, this.vrtx);
fill(0, 0, 0);
textSize(27);
textAlign(LEFT, TOP);
text(this.label, this.x + 10, this.y + this.height/4);
};
var btn1 = new Button(/*customize button*/);
//class selection
if (mage === true) {
var lvl = new character(10, 5, 10, 20, 5, 5);
manaLvl = 3;
strengthLvl = 1;
stealthLvl = 1;
} else if (warrior === true) {
var lvl = new character(20, 20, 5, 20, 5, 5);
manaLvl = 1;
strengthLvl = 3;
stealthLvl = 1;
} else if (rogue === true) {
var lvl = new character(15, 10, 10, 20, 5, 5);
manaLvl = 1;
strengthLvl = 2;
stealthLvl = 2;
} else if (cleric === true) {
var lvl = new character(15, 10, 15, 5, 10, 15);
manaLvl = 2;
strengthLvl = 2;
stealthLvl = 1;
}
var draw = function() {
btn1.draw();
};
如果有人知道是什麼問題是我會非常感謝你的幫助!
當我運行你的第一個片段,並檢查瀏覽器的控制檯,錯誤顯示爲上「Button」函數的第一行,因爲它認爲'config'是未定義的 - 在該代碼段中,它是未定義的。您的第二個較長的代碼段不起作用,因爲您有時會使用大寫的'C'拼寫'Character',有時使用小寫字母,所以它甚至沒有得到「Can not read property'x'of undefined」。錯誤。 – nnnnnn
什麼代碼進入'/ *自定義按鈕* /'行? – Oberon