英文:
I'm trying to figure out why this is constantly repeating instead of repeating at the specified amount of time in the line repeat
问题
我正在尝试创建一个代码,它会在随机位置创建随机数量的仙人掌,仙人掌由多条线组成。然而,似乎它在随机区域无限创建自身。我想知道为什么我的重复代码实际上没有重复指定的数量。
我的代码:
function setup() {
createCanvas (windowWidth, windowHeight);
}
function draw() {
repeat(1, cactus);
}
function cactus(){
const x = randomNumber (0,windowWidth);
const y = randomNumber (0, windowHeight);
line (x + 300, y + 275, x + 300, y + 250); //主干
line (x + 300, y + 258, x + 308, y + 258); //右侧分支1
line (x + 308, y + 258, x + 308, y + 254); //右侧分支2
line (x + 300, y + 265, x + 295, y + 265); //左侧分支1
line (x + 295, y + 265, x + 295, y + 255); //左侧分支2
}
如果你有关于代码或其他问题的进一步疑问,请随时提出。
英文:
I'm trying to create a code that creates random amount of a cactus in random locations which is made up of several lines. However it seems like it is infinitely creating itself in random areas. And I was wondering why my repeat code doesn't actually repeat the specific amount?
My code:
function setup() {
createCanvas (windowWidth, windowHeight);
}
function draw() {
repeat(1, cactus);
}
function cactus(){
const x = randomNumber (0,windowWidth);
const y = randomNumber (0, windowHeight);
line (x + 300, y + 275, x + 300, y + 250); //main branch
line (x + 300, y + 258, x + 308, y + 258); //right branch1
line (x + 308, y + 258, x + 308, y + 254); //right branch2
line (x + 300, y + 265, x + 295, y + 265); //right branch1
line (x + 295, y + 265, x + 295, y + 255); //right branch2
}
答案1
得分: 1
函数draw()
是一个循环,因此每秒重复多次;在这里绘制仙人掌会导致绘制非常多的仙人掌。如果你想控制绘制的数量,最好在draw()
函数之外进行控制;setup()
函数是一个很好的替代方案。我无法在Chrome的p5.js WebEditor中让'repeat()'
起作用,所以在下面的演示中我将其去掉了:
var numCacti = 50;
function setup() {
createCanvas(800, 600);
background(209);
for(let i = 0; i < numCacti; i++){
cactus();
}
}
function draw() {
}
function cactus(){
const x = random(0, 400);
const y = random(0, 200);
line(x + 300, y + 275, x + 300, y + 250);
line(x + 300, y + 258, x + 308, y + 258);
line(x + 308, y + 258, x + 308, y + 254);
line(x + 300, y + 265, x + 295, y + 265);
line(x + 295, y + 265, x + 295, y + 255);
}
英文:
Function draw() is a loop and therefore repeats several times a second; drawing the cacti here will cause a very large number to be drawn. If you want to control the number being drawn it will be better to do that outside of function draw(); function setup() would be a good alternative. I was unable to get 'repeat()' to work in the p5.js WebEditor in Chrome so I left it out of the following demo:
var numCacti = 50;
function setup() {
createCanvas(800, 600);
background(209);
for(let i = 0; i < numCacti; i++){
cactus();
}
}
function draw() {
}
function cactus(){
const x = random(0, 400);
const y = random(0, 200);
line(x + 300, y + 275, x + 300, y + 250);
line(x + 300, y + 258, x + 308, y + 258);
line(x + 308, y + 258, x + 308, y + 254);
line(x + 300, y + 265, x + 295, y + 265);
line(x + 295, y + 265, x + 295, y + 255);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论