【问题标题】:Cannot read property 'x' of undefined [closed]无法读取未定义的属性“x”[关闭]
【发布时间】:2016-10-05 02:13:08
【问题描述】:

所以我一直在尝试制作一个简单的基于文本的 RPG,我刚刚完成了为按钮和不同类型的角色创建对象。当我停止一天并保存时,我没有任何错误,但我今天回来并收到错误“无法读取未定义的属性'x'”。该错误没有列出任何代码行,除此之外我没有列出任何错误。我只在 Button 对象中使用了“x”,所以我认为问题出在这段代码中:

//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();
};

如果有人知道问题是什么,我将非常感谢您的帮助!

【问题讨论】:

  • 当我运行你的第一个 sn-p 并检查浏览器的控制台时,错误显示在 Button 函数的第一行,因为它认为 config 是未定义的 - 并且在那个sn-p,它是未定义的。你的第二个更长的 sn-p 不起作用,因为你有时用大写 C 拼写Character,有时用小写,所以它甚至没有达到“无法读取未定义的属性'x' 。”错误。
  • /*customize button*/ 行中的代码是什么?

标签: javascript object methods


【解决方案1】:

当你打电话时

var btn1 = new Button( /*customize button*/ );

Button 应该发送一个配置值。因为这不存在,所以当您尝试访问 config.x 时会收到错误消息。

解决问题需要将相关数据发送至Button如:

var btn1 = new Button( {x : 1, y : 2} ); // 等等

【讨论】:

猜你喜欢
  • 2021-04-17
  • 2019-04-09
  • 2019-09-29
  • 1970-01-01
  • 2022-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-28
相关资源
最近更新 更多