2016-08-01 43 views
0

收到「無法讀取屬性‘’我有這兩個對象構造和數組:創建和推動對象數組和不確定

//Houses 
var houseArray = []; 

//Generate street adress 
var houseStreets = [..., ..., ...]; 

var generateAddress = function(){ 
    var index = round(random(0, houseStreets.length)); 
    var num = round(random(0, 9999)); 
    return num + " " + houseStreets[index]; 
}; 

// House constructor and prototypes 
var House = function(address, x, y) { 
    this.address = address; 
    this.x = x; 
    this.y = y; 
}; 

House.prototype.draw = function() { 
    rectMode(CENTER); 
    rect(this.x, this.y, 30, 20); 
    rectMode(CORNER); 
}; 


//Households 

var householdArray = []; 

//Generate household names 
var householdNames = [..., ..., ...]; 

var generateName = function(){ 
    var index = round(random(0, householdNames.length)); 
    return householdNames[index]; 
}; 

//Household constructor and prototypes 
var Household = function(name, house) { 
    this.name = name; 
    this.house = house.address; 
    this.x = house.x; 
    this.y = house.y; 
    this.isHomeOwner = true; 
    this.isSelling = false; 

}; 

Household.prototype.draw = function() { 
    ellipse(this.x, this.y, 10, 10); 
}; 

Household.prototype.determinMove = function() { 
    if(random(0, 100) <= 75){ 
     this.isSelling = true; 
    } 
}; 

在我的模擬的初始階段創建20戶和20間房屋和20戶居住在房子20:

// Populate initial houses 
noFill(); 
houseArray.push(new House(generateAddress(), 15, 10)); 
houseArray.push(new House(generateAddress(), 50, 10)); 
houseArray.push(new House(generateAddress(), 85, 10)); 
houseArray.push(new House(generateAddress(), 120, 10)); 
houseArray.push(new House(generateAddress(), 155, 10)); 
houseArray.push(new House(generateAddress(), 190, 10)); 
houseArray.push(new House(generateAddress(), 225, 10)); 
houseArray.push(new House(generateAddress(), 260, 10)); 
houseArray.push(new House(generateAddress(), 295, 10)); 
houseArray.push(new House(generateAddress(), 330, 10)); 
houseArray.push(new House(generateAddress(), 15, 389)); 
houseArray.push(new House(generateAddress(), 50, 389)); 
houseArray.push(new House(generateAddress(), 85, 389)); 
houseArray.push(new House(generateAddress(), 120, 389)); 
houseArray.push(new House(generateAddress(), 155, 389));   
houseArray.push(new House(generateAddress(), 190, 389)); 
houseArray.push(new House(generateAddress(), 225, 389)); 
houseArray.push(new House(generateAddress(), 260, 389)); 
houseArray.push(new House(generateAddress(), 295, 389)); 
houseArray.push(new House(generateAddress(), 330, 389)); 

//Populate initial households 
for (var i = 0; i < houseArray.length; i++){ 
    householdArray.push(new Household(generateName(), houseArray[i])); 
} 

現在,我試圖建立一個將在每一個新的轉折,將創建不隨地駐留5周新的家庭的開始(被調用的函數沒有被綁在房子的房子裏),並且有一個隨機的x(在...之間) n 10和390)和隨機y(50和350之間)的位置,我似乎無法使其工作。這裏是我想出了代碼:

var newArrival = function() { 
    for (i = 0; i < 5; i++) { 
    householdArray.push(new Household(generateName())); 
    } 
}; 

但是當我做print(householdArray[22].name)我得到一個錯誤「未定義無法讀取屬性‘名’」。另外我還沒有想出如何處理隨機的x和y位置。

問題:我的代碼出了什麼問題?建立一個能創建5戶家庭陣營並且隨機x(10到390)和隨機y(50到350)職位的職能的函數是一個好方法嗎?

更新:我相信問題是與Household構造函數。這似乎不能很好地處理house參數沒有提供參數的情況,我不知道如何解決這個問題。在我的newArrival函數中,我不提供house參數,因爲沒有這個參數,新來的人還沒有房子。這似乎造成了一個問題。任何想法如何解決這個問題?

+0

調試器說你得到那個錯誤在哪裏?它應該給你一個文件名和一個行號。 – zero298

+0

您的代碼似乎只將5個項目推送到'householdArray'。如果是這樣的話,'householdArray [22]'自然會返回'undefined'。 – acdcjunior

+0

@acdcjunior它可能並不容易看到,但在初始設置中,我已經在'householdArray'中生成了20個項目。我的'newArrival'函數是爲了增加5個。 – neoflash

回答

0

問題確實與構造函數有關。出於某種原因,我不完全理解,我不得不告訴構造函數在沒有提供參數house的情況下該怎麼做。修復它,一切正常。這裏是代碼:

var Household = function(name, house) { 
    if (house === undefined) { 
     this.name = name; 
     this.house = "HOMELESS"; 
     this.x = random(10, 390); 
     this.y = random(50, 350); 
     this.isHomeOwner = false; 
     this.isSelling = false; 
     this.askingPrice = 0; 
    } else { 
     this.name = name; 
     this.house = house; 
     this.x = house.x; 
     this.y = house.y; 
     this.isHomeOwner = true; 
     this.isSelling = false; 
     this.askingPrice = 0; 
    } 
};