无法读取未定义[已解决]

时间:2019-03-24 06:03:10

标签: javascript

我创建的胸部对象具有更新功能,但是显然它们不存在或未声明?

我感觉我一直在努力地检查我的工作,但是它很复杂,甚至可能超出我的水平,并且不知道出了什么问题,对我的代码进行了注释,以使您的工作更加轻松,因为我没有太多帮助< / p>

/*
 * Important Variables
 */
//Colors for inventory slots
var inventoryColor1 = (150, 150, 150);
var inventoryColor2 = (60, 60, 60);

//Function Prototypes
var Player;
var InventoryManager;

// Neutral
var InventorySlot;

var Chest;
var ChestCreator;

var Chest = function(x, y, w, h, r)
{
  this.Slots = [];
  this.x = x;
  this.y = y;
  this.w = w;
  this.h = h;
  this.r = r;
  this.color1 = (201, 111, 0);
  this.color2 = (255, 234, 0);
  this.color3 = (15, 11, 15); 
  this.inventoryActive = true;

  //Create 4 slots on startup
  for (var i = 0; i < 4; i++)
  {
   this.Slots.push(InventorySlot(this.x, this.y));   
  }

 //Create new slots 
  this.newSlot = function()
  {
      this.Slots.push(new InventorySlot(this.x, this.y));
  };

  this.drawSlots = function()
  {
       if (this.inventoryActive)
       {  
          // if the slots are active, draw them
          // Set the 'curser' up and to the left to start drawing slots
          // (currentSlot.x = currentObject.x-slotW*currentObject.slots.length/4)
          this.cx = this.x-28*this.Slots.length/4;
          this.cy = this.y-28*this.Slots.length/4;

          for (var i = 0; i < this.Slots.length; i++)
          {
                fill(inventoryColor2);
                strokeWeight(3);
                stroke(inventoryColor1);
                rect(this.cx-3,this.cy-3,25+3,25+3);
                fill(inventoryColor1);
                noStroke();
                rect(this.cx, this.cy, 25, 25, 5);

                // Create rows of 4
                this.cx+=28;
                if (i%4 === 0)
                {
                    this.cy+=28;  
                    this.cx = this.x-28*this.Slots.length/4;
                }
           }
       }
  };

// 
//
//Pretty sure this is there the problem is
//
//
  this.update = function()
  {
      stroke(this.color1);
      fill(this.color2);
      rect(this.x, this.y, this.w, this.h, this.r);
      noStroke();
      fill(this.color3);
      rect(this.x,this.y+height/2,this.w,this.h/5);
  };  
};

// A creater of the chest objects, 

var ChestCreator = function()
{
 // Chests in the enviornment
 this.Chests = [];

/*    
Removing this from the code (entirely) makes the program run smoothly, but im sure its essential
*/
 // Create 4 chests on Startup
  for (var i = 0; i < 4; i++)
  {
      this.Chests.push(Chest(random(25,375), random(25,375), random(20,25), random(15,20), random(0,5)));
  }

  this.newChest = function()
  {
     // generate a chest with random x,y,width,height,radius
     this.Chests.push(Chest(random(25,375), random(25,375), random(20,25), random(15,20), random(0,5)));
  };

  this.updateChests = function()
  {
      for (var i = 0; i < this.Chests.length; i++)
      {
// 
//
//Pretty sure this is there the problem is
//
//
           this.Chests[i].update();
           this.Chests[i].drawSlots();
      }
  };  
};

var InventorySlot = function(x, y, manager)
{
    this.x = x;
    this.y = y;
    this.w = 20;
    this.h = 20;
    this.r = 5;
    this.color1 = inventoryColor1;
    this.color2 = inventoryColor2;
    this.manager = manager;

    this.getNewItem = function(item)
    {
        this.item = item;
    };

    // trade items with another slot, using ITS inventory manager's 'holder' variable
    this.replaceItem = function(otherSlot)
    {
        this.otherSlot = otherSlot;         
        this.manager.holder = this.otherSlot.item;
        this.otherSlot.item = this.item;
        this.item = this.manager.holder;
    };
};

// Important Objects
var player = new Player();

// one manages the chests in the world, one manages the slots in the chests!
var chestCreator = new ChestCreator();

var draw = function() 
{
    background(0, 0, 0);
    chestCreator.updateChests();
};

任何帮助都是-非常感谢,因为我只是一个高中生,他只是想学习如何使用代码竖起大拇指

预期  -应出现4个宝箱,上方有四个存货槽  -更多的箱子应该可以轻松添加,并且更多的插槽应该可以  随意添加到每个箱子 实际-出现了来自播放器的4个插槽(仍然损坏,但不是当前问题),然后中断,表示未定义“更新”

2 个答案:

答案 0 :(得分:0)

尝试更改

this.Chests.push(Chest(random(25,375), random(25,375), random(20,25), random(15,20), random(0,5)));

// note the "new" keyword!
this.Chests.push(new Chest(random(25,375), random(25,375), random(20,25), random(15,20), random(0,5)));

new关键字对于正确构造具有正确上下文的对象很重要。它还可以正确返回一个Chest对象,以将其推送到您的数组。

您可以在以下Stackoverflow答案https://stackoverflow.com/a/30478240/11240618以及此处:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new

中找到更多信息。

答案 1 :(得分:0)

在创建新的Chest对象时,必须使用new运算符。所以在这段代码中:

// Create 4 chests on Startup
for (var i = 0; i < 4; i++)
{
      this.Chests.push(Chest(random(25,375), random(25,375), random(20,25), random(15,20), random(0,5)));
}

...您必须更改

this.Chests.push(Chest(random(25,375), random(25,375), random(20,25), random(15,20), random(0,5)));

...对此:

this.Chests.push(new Chest(random(25,375), random(25,375), random(20,25), random(15,20), random(0,5)));