Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法读取属性';更新';未定义[已解决]_Javascript - Fatal编程技术网

Javascript 无法读取属性';更新';未定义[已解决]

Javascript 无法读取属性';更新';未定义[已解决],javascript,Javascript,我创建的胸部对象有一个更新函数,但显然它们不存在或者没有声明 我觉得我一直在仔细检查我的工作,但它相当复杂,可能超出我的理解范围,我不知道出了什么问题,我的代码被注释了,以便让你的工作更容易,因为我没有太多的帮助 /* * Important Variables */ //Colors for inventory slots var inventoryColor1 = (150, 150, 150); var inventoryColor2 = (60, 60, 60); //Functi

我创建的胸部对象有一个更新函数,但显然它们不存在或者没有声明

我觉得我一直在仔细检查我的工作,但它相当复杂,可能超出我的理解范围,我不知道出了什么问题,我的代码被注释了,以便让你的工作更容易,因为我没有太多的帮助

/*
 * 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();
};
/*
*重要变量
*/
//库存插槽的颜色
var inventoryColor1=(150150150);
var InventoryColory2=(60,60,60);
//功能原型
var播放器;
var清单管理器;
//中立的
var目录槽;
胸廓;
创造者;
var胸部=功能(x、y、w、h、r)
{
这个.Slots=[];
这个.x=x;
这个。y=y;
这个.w=w;
这个,h=h;
这个。r=r;
this.color1=(201111,0);
this.color2=(255234,0);
this.color3=(15,11,15);
this.inventoryActive=true;
//启动时创建4个插槽
对于(变量i=0;i<4;i++)
{
this.Slots.push(InventorySlot(this.x,this.y));
}
//创建新插槽
this.newSlot=函数()
{
this.Slots.push(newinventorySlot(this.x,this.y));
};
this.drawSlots=函数()
{
if(this.inventoryActive)
{  
//如果插槽处于活动状态,请绘制它们
//将“游标”向上和向左设置,以开始绘制槽
//(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;
对于(var i=0;i
任何帮助都是非常感谢的,因为我只是一个高中生,正试图学习如何用代码大拇指做伟大的事情

期望 -应显示4个箱子,上面有库存槽,4个槽 -更多的箱子应该容易添加,更多的插槽应该可以添加 随意添加到每个箱子中 实际-从播放器(仍然损坏但不是当前问题)对象中显示4个插槽,然后它会断开,表示“更新”未定义)

尝试更改

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

new
关键字对于正确构建具有正确上下文的对象非常重要。它还正确地返回要推送到数组中的
胸部
对象


您可以使用此Stackoverflow答案找到更多信息:,也可以在此处找到:

当您创建一个新的
胸部
对象时,您必须使用
新建
操作符。所以在这段代码中:

// 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(new Chest(random(25,375), random(25,375), random(20,25), random(15,20), random(0,5)));

代替了
.push(胸部(
do
).push(新胸部(
)?也许-因为调用
胸部
会导致未定义,实例化
新胸部
会导致胸部你应该做“新胸部(…)”)谢谢伙计们…我做得比较早,编辑一直告诉我我缺少逗号等等。但它不起作用,谢谢
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)));