简体   繁体   English

for 循环不起作用 p5.js

[英]A for loops aren't working p5.js

 var Background; function setup() { createCanvas(windowWidth, windowHeight); Background = color(0); background(Background); for(var i = 0; i < 400; i += 25) { //print("i " + i); for(var u = 0; i < 800; i += 25) { noFill(); stroke(255, 255, 255); rect(i, u, 25, 25); //print("u " + u); } } } function draw() { } function windowResized() { resizeCanvas(windowWidth, windowHeight); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.0/p5.js"></script>

I am making a game that requires cells and I started debugging because the cells weren't working and this is what I got.我正在制作一个需要单元格的游戏,我开始调试,因为单元格不工作,这就是我得到的。 Any idea what is wrong with it?知道它有什么问题吗?

Please be more specific than saying it doesn't work.请比说它不起作用更具体。 What exactly did you expect this code to do?您究竟期望这段代码做什么? What exactly happens instead?究竟会发生什么? Which line of code is different from what you expected?哪一行代码与您预期的不同? Unless you can provide that information, your question is probably too broad for Stack Overflow.除非你能提供这些信息,否则你的问题对于 Stack Overflow 来说可能太宽泛了。 But I'll try to help you in a general sense.但我会尽量帮助你在一般意义上。

You need to get into the habit of debugging your code .您需要养成调试代码的习惯。 (That tutorial is for Processing, but many of the same ideas apply to P5.js and JavaScript.) (该教程适用于处理,但许多相同的想法适用于 P5.js 和 JavaScript。)

I see that you added a couple print statements in your program, which is good, but you need to read what they say to understand what your program is doing.我看到您在程序中添加了几个打印语句,这很好,但是您需要阅读他们所说的内容以了解您的程序在做什么。 You should also read through your code carefully, line by line, with some sample input and a piece of paper and a pencil to track variables.您还应该逐行仔细阅读您的代码,并使用一些示例输入和一张纸和一支铅笔来跟踪变量。

Anyway, either way you should notice that your u variable is always 0 .无论如何,无论哪种方式,您都应该注意到您的u变量始终为0 This is probably not what you expected.这可能不是您所期望的。

This would tell you to take a closer look at the for loop that uses the u variable:这将告诉您仔细查看使用u变量的for循环:

for(var u = 0; i < 800; i += 25) {

And hopefully now it becomes obvious that you're switching variables here.希望现在很明显你在这里切换变量。 This line should probably be:这一行应该是:

for(var u = 0; u < 800; u += 25) {

In the future please try to do this type of debugging before you post.以后请在发帖前尝试进行此类调试。 It will save you a lot of time.它会为您节省很多时间。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM