如何解决为什么我的for循环不访问该对象内的数组?
我正在尝试绘制橙色椭圆,但是代码底部的for循环未绘制任何内容。我知道下面的代码是访问对象内部数组的正确方法,因为我在另一个任务中使用了它并且它可以工作。我以前有beginShape();和endShape();在我的for循环之前和之后,但这也不起作用。
一切看起来都正确,我不确定自己在做什么错。谢谢大家的帮助。谢谢
Case 601 - Cross Reference - stage 2
Fry is still on the loose. We think she’s resorted to stealing to get by.
Hopefully we can track her down by cross-referencing sightings and recent thefts in the area.
In the setup function,use a for loop to traverse the sightings,marking all of the locations on the map
where she was last seen. Do this by drawing small,DarkOrange stroke ellipses at each location.
In addition,we've assembled a list of recent thefts in the area. Using another for loop to traverse the
recent crime records,you should mark those locations on the map. Do this by drawing small,Magenta stroke rectangles centered over each location.
Use X11 colours. You can find a reference table at https://en.wikipedia.org/wiki/Web_colors.
For this mission you will need ONLY the following:
- for loop
- stroke
- ellipse()
- stroke
- rect() NB. Draw each rectangle with the point at its center.
*/
var countyMap;
//Sightings of Casey Fry.
var absconder_record = {
Loc_X: [639,681,712,756,715,701,753,815,795,788,781,768,750,732,714,695,693,654,624,594,555],Loc_Y: [288,286,293,310,368,425,436,468,506,497,486,489,500,514,531,552,523,484,474],};
//Recent crime records.
var robbery_record = [
{ PointX : 403,PointY : 401},{ PointX : 402,PointY : 360},{ PointX : 427,PointY : 403},{ PointX : 646,PointY : 284},{ PointX : 639,PointY : 264},{ PointX : 830,PointY : 434},{ PointX : 809,PointY : 443},{ PointX : 844,PointY : 496},{ PointX : 802,PointY : 350},{ PointX : 683,PointY : 413},{ PointX : 552,PointY : 464},{ PointX : 629,PointY : 498},{ PointX : 712,PointY : 562},{ PointX : 783,PointY : 603},{ PointX : 415,PointY : 225},{ PointX : 561,PointY : 282},{ PointX : 562,PointY : 392},{ PointX : 751,PointY : 283},{ PointX : 680,PointY : 359},{ PointX : 626,PointY : 436},{ PointX : 701,PointY : 455},{ PointX : 838,PointY : 565},{ PointX : 322,PointY : 508},{ PointX : 468,PointY : 556},{ PointX : 625,PointY : 737}
];
function preload()
{
countyMap = loadImage("map.png")
}
function setup()
{
createCanvas(countyMap.width,countyMap.height);
image(countyMap,0);
//add your code below here
stroke(255,140,0);
for(i = 0; i <absconder_record.Loc_X[i].length; i++ ){
ellipse(absconder_record.Loc_X[i],absconder_record.Loc_Y[i],5)
}
}
解决方法
看着i <absconder_record.Loc_X[i].length
,
-
absconder_record.Loc_X
是一个数组 -
absconder_record.Loc_X[i]
是一个数字 -
absconder_record.Loc_X[i].length
是数字length
的{{1}}属性,该数字是undefined
-
对小于
undefined
的数字进行比较检查将返回false
因此循环条件总是失败,并且循环体永远不会执行。
,查看此行:
for(i = 0; i <absconder_record.Loc_X[i].length; i++ ){
我认为您的意思是:
i <absconder_record.Loc_X.length
i <absconder_record.Loc_X[i].length
-
i
为0 -
absconder_record.Loc_X[0]
是639
-
639
没有length
属性。
关注评论中的问题:
如何遍历数组robbery_record?
for loop包含3个表达式:
- 初始化程序,通常用于初始化计数器,如
i = 0;
中所示。 (这只运行一次,而不是每次迭代。) - 要测试的条件。如果条件为真,则循环主体运行。最常见的用途是查看计数器是否已超过某个阈值,例如
i > 10
。如果i
小于10,请运行循环的正文。 - 在循环的每次迭代之后运行的表达式。最常用的递增计数器:
i++
。
在一起看起来像这样:
for (let i = 0; i < 10; i++) {
// body
}
-
i
以0
开头。 - 评估条件:我是否小于10?
- 如果是这样,循环的主体就会运行。
- 正文运行后,将执行最后一个表达式(
i++
)。现在我是1。 - 重复2-4,直到条件不再成立为止(当我不小于10时)。
要使用for循环遍历数组,通常的方法与上述方法完全相同,但是将10
替换为数组的长度。因此,在您的情况下使用robbery_record:
for (let i = 0; i < robbery_record.length; i++) {
// do stuff
}
通常,当有人这样做时,是因为他们想对数组中的每个项目执行一次主体。方便地,由于i
每次在循环中都会递增,因此您可以通过robbery_record[i]
在循环体内获取当前项:
for (let i = 0; i < robbery_record.length; i++) {
const record = robbery_record[i];
// do stuff with the current record
}
每个robbery_record条目都是一个具有PointX
和PointY
属性的对象,因此您可以在循环体中使用它们:
for (let i = 0; i < robbery_record.length; i++) {
// record is an object,like: { PointX: 123,PointY: 456 }
const record = robbery_record[i];
// do stuff with the object's properties
console.log(record.PointX) // 123
console.log(record.PointY) // 456
const x = record.PointX; // now x === 123
const y = record.PointX; // now y === 456
// etc.
}
希望这会有所帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。