[英]This is an infinite loop, but I cannot for the life of me figure out why
So pretty much I'm making a little web app to get better with using the canvas, but I'm stuck. 因此,我几乎在制作一个小型Web应用程序,以更好地使用画布,但我遇到了麻烦。 I would like a rotating n-sided polygon (the drawing of lines already works). 我想要一个旋转的n面多边形(线条绘制已经起作用)。 The game loop loops through a grid array (each point on the grid holds a subclass of a Point() object), and calls the tick() method on each. 游戏循环循环遍历一个网格数组(网格上的每个点都包含Point()对象的子类),并在每个循环上调用tick()方法。 Everything works fine until it hits a ShapePoint() object (middle mouse to place on canvas). 一切正常,直到碰到ShapePoint()对象(鼠标中键放置在画布上)为止。 The ShapePoint's tick() method is somehow an infinite loop. ShapePoint的tick()方法某种程度上是一个无限循环。 If you put a console.log("hi") inside it, it will give you about 2000 "hi" messages, so it's working (in theory). 如果将console.log(“ hi”)放入其中,它会为您提供大约2000条“ hi”消息,因此它可以正常工作(理论上)。 The funny bit is, even though it is looping through stoke()'es, nothing is happening! 有趣的是,即使它遍历了stoke(),也没有任何反应!
//################################################################
// THIS IS THE PROBLEM CLASS.
// So pretty much, when the game loop calls the tick() funciton
// of ANY ShapePoint object, everything hangs. The game is still
// looping through the ENTIRE tick() function (put console.log()
// functions in and you'll see what I mean) continually, but the
// effects it is supposed to display aren't shown.
//
//################################################################
function ShapePoint(x, y, sides) {
//position variable
this.positionOnCanvas = [x, y];
//number of sides
this.N = sides;
//current step
this.step = 0;
//the array to store all the angle data
this.thetas = new Array(this.N);
//the array to store all the vertex data
this.vertList = new Array(this.N);
//function to increase all the angels by an even amount
this.stepPoints = function(s) {
//for every side
for (i=0; i<this.N; i++) {
//multiply the current 'i' value by ((360/number of sides) + current step). This serves to create points at even intervals all the way around a circle, and have it increase by s every loop
this.thetas[i] = i*((360/this.N) + s);
//get the x value with 40*cos(angle for this point). Same for y, only with sin. Round both to 2 decimal places
this.vertList[i] = [Math.round((40*(Math.cos(this.thetas[i])))*100)/100, Math.round((40*(Math.sin(this.thetas[i])))*100)/100];
//if the current angle is between 90 and 180...
if (this.thetas[i]>=90 && this.thetas[i]<=180) {
//invert the x value
this.vertList[i][0] *= -1;
//else if the angle is between 180 and 270...
} else if (this.thetas[i]>=180 && this.thetas[i]<=270) {
//invert both the x and the y values
this.vertList[i][0] *= -1;
this.vertList[i][1] *= -1;
//else if the angle is between 270 and 360...
} else if (this.thetas[i]>=270 && this.thetas[i]<=360) {
//invert the y value
this.vertList[i][1] *= -1;
}
//nothing needed for 0-90 because both are positive
}
}
this.tick = function() { //<<<<<<<<THIS IS THE PROBLEM FUNCTION!
//setup all the points forward
this.stepPoints(this.step);
//for every side in this polyogn...
for (i=0; i<this.N; i++) {
//shorten the location of the positions
var posX = this.vertList[i][0] + this.positionOnCanvas[0];
var posY = this.vertList[i][1] + this.positionOnCanvas[1];
//begin drawing
ctx.beginPath();
//move to the x and y location of the current point
ctx.moveTo(posX, posY);
//if point is not the last in the array...
if (i < this.N-1) {
//draw a line to the next point in the array
ctx.lineTo(this.vertList[i+1][0] + this.positionOnCanvas[0], this.vertList[i+1][1] + this.positionOnCanvas[1]);
//else...
} else {
//draw a line to the first point in the array
ctx.lineTo(this.vertList[0][0] + this.positionOnCanvas[0], this.vertList[0][1] + this.positionOnCanvas[1]);
}
//draw a line
ctx.strokeStyle = "#000000";
ctx.lineWidth = 0.5;
//end
ctx.stroke();
//draw the vertex
ctx.fillStyle = "orange";
ctx.fillRect(posX-2, posY-2, 4, 4);
}
//draw the origin of the polygon
ctx.fillStyle = "lightPurple";
ctx.fillRect(this.positionOnCanvas[0]-2, this.positionOnCanvas[1]-2, 4, 4);
//if the step is greater than 360, set it to 0
this.step = this.step % 36; //(thanks Neikos!)
}
}
ShapePoint.prototype = new Point();
So I've spent hours tweaking different things, and I cannot for the life of me see what the problem is! 所以我花了几个小时来调整不同的东西,而我一生都无法看到问题所在! If anybody can figure it out, it would be fantastic. 如果有人能弄清楚,那就太好了。 If you need more context as to how exactly this is implemented, I've created a JSFiddle for you. 如果您需要更多有关如何实现此功能的上下文,我为您创建了一个JSFiddle 。 Thanks in advance, this place is always so helpfull! 在此先感谢,这个地方总是那么有用!
EDIT :: I do realize my code is a bit clunky, but I typing out what everything does really helps me learn for the next time 编辑::我确实意识到我的代码有点笨拙,但是我输入了所有的内容,这确实有助于我下次学习
user2310289 is correct in his/her comment above: you're using a single global i
variable in both stepPoints
and tick
, so these methods are interfering with each other. user2310289在上面的评论中是正确的:您在stepPoints
和tick
中都使用了一个全局i
变量,因此这些方法相互干扰。
There are some languages where a variable used in a method is implicitly local unless declared otherwise, but JavaScript is not one of those languages. 在某些语言中,除非另行声明,否则方法中使用的变量是隐式局部的,但JavaScript并不是其中的一种。 In JavaScript you need to use the var
keyword to declare your local variables, otherwise they are implicitly global. 在JavaScript中,您需要使用var
关键字声明局部变量,否则它们是隐式全局的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.