【发布时间】:2020-02-09 08:45:02
【问题描述】:
我在靠近底部的代码行中收到一条非法的返回语句:返回点
createPoints(x, y, length, depth, angle, points)
{
if(depth > 0)
{
//draws line
points.push((x + length) * Math.sin(angle));
points.push((y + length) * Math.cos(angle));
//draw left branch
angle += Math.PI / 4;
createPoints(treeString, (x + length/2) * Math.sin(angle), (y + length/2) * Math.cos(angle), depth - 1, points);
//goes back
points.push(x);
points.push(y);
//draw right branch
angle -= Math.PI / 2;
createPoints(treeString, (x + length/2) * Math.sin(angle), (y + length/2) * Math.cos(angle), depth - 1, points);
return points;
}
return;
}
该函数应该在数组中绘制点,以便在 webgl 中使用分形树。我不确定我为什么会出错,不幸的是,我的教授和助教都不知道。
【问题讨论】:
-
由于您只能在Function 中使用
return,因此您必须声明createPoints,即function createPoints(x, y, length, depth, angle, points) { // function body ... }。检查有关函数的 MDN 链接并查看声明函数的替代方法。 -
你能显示完整的错误(截图,如果需要的话)和更多的代码上下文吗?
-
哦,这段代码只是在顶层吗?您正在尝试使用仅适用于类和对象字面量的简写。需要有某种声明,就像@NikKyriakides 说的那样。
-
能否分享整个代码以便我们调试?
标签: javascript recursion return