简体   繁体   English

我在将 function 添加到数组时遇到问题。 (JavaScript)

[英]I have an issue with adding a function to an array. (JavaScript)

This is the code that adds the Function (Canvas Drawing) To the sameTree array这是将 Function (Canvas Drawing) 添加到 sameTree 数组的代码

let TreesI = 5;
    let sameTree = [];
    let randomValueX = mathR(-20, 20);
    let randomValueY = mathR(-20, 20);
    for (let i = 0; i < TreesI; i++) {
      randomValueX = mathR(-20, 20);
      randomValueY = mathR(-20, 20);
      sameTree[TreesI] = Tree7(randomValueX, randomValueY)
    }

But it didn't do anything,但它什么也没做,

The console.log output gave this console.log output 给出了这个

[5: undefined] (6) = $2 [5: 未定义] (6) = $2

This is a sample of Tree7这是 Tree7 的示例

function Tree7(startWidth, startHeight) {

  ctx.save();
  let treeObj = {
    translateValues: {
      startWidth: startWidth,
      startHeight: startHeight,
      endWidth: -startWidth,
      endHeight: -startHeight
    },
    scaleR: mathR(0.8, 1)
  }

  ctx.translate(treeObj.translateValues.startWidth, treeObj.translateValues.startHeight);
ctx.scale(treeObj.scaleR,treeObj.scaleR);

//ctx.strokeStyle = 'rgb(0, 100, 1)';
ctx.strokeStyle = 'black';
ctx.globalAlpha = 0.5;
ctx.lineWidth = 0.2;
var reduce = 2;
  //Tree drawing sample under
  ctx.beginPath();
  ctx.moveTo(0,0);
  ctx.lineTo(mathR(0,10),0);
  ctx.stroke();
  // Tree drawing sample above

  ctx.translate(treeObj.translateValues.endWidth, treeObj.translateValues.endHeight);
  ctx.restore();
}

Now after getting some answers I realised that I need to stop updating the random value ctx.lineTo(mathR(0,10),0);现在得到一些答案后,我意识到我需要停止更新随机值ctx.lineTo(mathR(0,10),0); . . The code I have currently just keeps the coordinates.我目前的代码只保留坐标。 Please help.请帮忙。

If you are writing sameTree[i] instead of sameTree[TreesI] it will work when Tree7(randomValueX, randomValueY) returns a function.如果你写的是sameTree[i]而不是sameTree[TreesI],它将在Tree7(randomValueX, randomValueY)返回function时工作。

Try this to store the randomly generated position values and draw.试试这个来存储随机生成的 position 值并绘制。

let TreesI = 5;
let sameTree = [];
let randomValueX = mathR(-20, 20);
let randomValueY = mathR(-20, 20);
for (let i = 0; i < TreesI; i++) {
   randomValueX = mathR(-20, 20);
   randomValueY = mathR(-20, 20);
   sameTree[i] = {"x":randomValueX, "y":randomValueY};
   Tree7(sameTree[i].x, sameTree[i].y);
}

And when you want to draw with pre-generated values after this,当您想在此之后使用预生成的值进行绘制时,

for (let i = 0; i < TreesI; i++) {
   Tree7(sameTree[i].x, sameTree[i].y);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在javascript中使用数组函数时遇到问题 - I have an issue with making an array function in javascript Javascript,表格和数组。 棘手的问题 - Javascript, table and array. tricky issue 我有数据存储在Perl数组中。 如何使用JSON将它们转换为JavaScript数组? - I have data stored in Perl array. How do I convert them into JavaScript array using JSON? JavaScript:我有一个数组。 我想检查第一个元素是否为字段集 - JavaScript: I have an array. I want to check if the first element is a fieldset Javascript 如何优化大阵列中的延迟。 它有 350000+ 项 - Javascript how can i do optimize the delay in big array. its have 350000+ item 在JSON数组中查找Item并将其添加到另一个Array中。 Java脚本 - Finding Item in JSON Array and adding it to another Array. Javascript 在数组中添加数字。 得到奇怪的输出。 Java脚本 - Adding numbers in an Array. Getting weird output. Javascript Javascript数组。 高级 - Javascript array. Advanced JavaScript中的一个返回数组的函数。 我可以在调用函数时打印数组中的项目,但不能打印数组中的每个项目 - A function in JavaScript that returns an array. I am able to print the items in the array when the function is called, but not each item in the array 使用Javascript时数组出现问题 - I have a issue with an array when using Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM