我正在尝试使用一系列书籍和嵌套for循环来绘制书架。 for循环绘制书架,书籍和标题以及书籍的星级(评级)。然而,在尝试访问星形变量时,我得到了“星星未定义的错误”。非常感谢任何帮助。
var appsPerShelf = 4;
var apps = [
{title: "My House", author:"Claire", stars: 4},
{title: "My Animal":, author: "Claire", stars: 5},
{title: "Funny Face", author: "Claire", stars: 2},
{title: "My Inital", author: "Claire", stars:3},
{title: "Dancing Animals", author:"Claire", stars: 1},
{title: "Racing Animals", author:"Claire", stars: 5},
{title: "Resize Animal", author: "Claire", stars: 3},
{title: "App Bookshelf", author: "Claire",stars: 2},
{title: "Ball Follow App", author:"Claire", stars: 3},
{title: "Red Soz Quiz App", author:"Claire", stars: 5},
{title: "Zoo App", author:"Claire", stars: 4},
{title: "Dry Animal App", author:"Claire", stars: 5},
{title: "Dancing Animals Fun", author:"Claire", stars: 3},
{title: "Racing Animals Fun", author:"Claire", stars: 1},
{title: "Red Sox Quiz", author: "Claire", stars: 3}
];
//shelf drawer
for(var i=0; i<14%appsPerShelf; i++){
fill(173, 117, 33);
rect(0, 20+(20*i), width, 10);
//app drawer
for(var j=0; j<appsPerShelf; j++){
fill(214, 255, 219);
rect(10, 20, 90, 100);
fill(0, 0, 0);
text(apps[j].title, 15+(20*i), 19+(20*i), 70, 100);
//star drawer
if(apps[i*appsPerShelf+j].stars){
for(var k=0; apps[i*appsPerShelf+j].stars; k++){
var img = getImage("cute/Star");
}}
}
}
答案 0 :(得分:0)
将您的行更改为此
for (var k = 0; k < apps[i * appsPerShelf + j].stars; k++)
e.g。
var appsPerShelf = 4;
var apps = [{
title: "My House",
author: "Claire",
stars: 4
},
{
title: "My Animal",
author: "Claire",
stars: 5
},
{
title: "Funny Face",
author: "Claire",
stars: 2
},
{
title: "My Inital",
author: "Claire",
stars: 3
},
{
title: "Dancing Animals",
author: "Claire",
stars: 1
},
{
title: "Racing Animals",
author: "Claire",
stars: 5
},
{
title: "Resize Animal",
author: "Claire",
stars: 3
},
{
title: "App Bookshelf",
author: "Claire",
stars: 2
},
{
title: "Ball Follow App",
author: "Claire",
stars: 3
},
{
title: "Red Soz Quiz App",
author: "Claire",
stars: 5
},
{
title: "Zoo App",
author: "Claire",
stars: 4
},
{
title: "Dry Animal App",
author: "Claire",
stars: 5
},
{
title: "Dancing Animals Fun",
author: "Claire",
stars: 3
},
{
title: "Racing Animals Fun",
author: "Claire",
stars: 1
},
{
title: "Red Sox Quiz",
author: "Claire",
stars: 3
}
];
//shelf drawer
for (var i = 0; i < 14 % appsPerShelf; i++) {
//app drawer
for (var j = 0; j < appsPerShelf; j++) {
console.log(apps[j].title);
//star drawer
var stars = "";
for (var k = 0; k < apps[i * appsPerShelf + j].stars; k++) {
stars += "*";
}
console.log(stars);
}
}