我不知道赋值运算符是如何工作的[处理]

发布于 2024-10-12 09:26:38 字数 1543 浏览 5 评论 0原文

我正在编写一个模拟蛇的程序,蛇的每个部分的值都存储在一个数组中。为了模拟蛇的物理原理,我需要将后续部分分配给前一个部分,这就是我遇到问题的地方。

int bar;
int foo;

void setup() {}

void draw() {
  foo = bar;
  bar = mouseX;
  println(foo);
  println(bar);
}

在这个例子中, foo 不应该输出 bar 之前的鼠标位置吗?在 foo 被分配给 bar 之后,Bar 被分配给 mouseX。赋值运算符是否使得如果被赋值的变量发生变化,被赋值的变量也会随之改变?这非常令人沮丧,我认为解决方案应该很简单。如何将一个变量分配给另一个变量,而不考虑所引用的 var 的未来更改?

编辑:这是一个蛇模拟器的示例,它应该可以工作,但出于同样的原因却不能,所有段在每个 for 循环结束时最终都是相同的:

int segments = int(random(3, 10));
float springing[] = new float[segments];
float damping[] = new float[segments];
PVector accel[] = new PVector[segments];
PVector[] joints = new PVector[segments];
PVector[] delta = new PVector[segments];
PVector food = new PVector(0,0,0);

void setup() {
  size(500, 500);
  stroke(255);
  for(int n = 0; n < joints.length; n++) {
    if (n == 0) joints[0] = new PVector(random(width), random(height));
    else joints[n] = joints[0];
    delta[n] = new PVector(0,0,0);
    accel[n] = new PVector(0,0,0);
    springing[n] = .05*(.07*(n+1)); 
    damping[n] = .95-(.02*n);
  }
}

void draw() {
  background(0);
  food.x =  mouseX;
  food.y = mouseY;
  for(int n = 0; n < segments; n++) {
    if (n == 0) {
     delta[0] = PVector.sub(food, joints[0]); 
     joints[0].add(delta[0]);
    }
    else {
     delta[n] = PVector.sub(joints[n-1], joints[n]);
     delta[n].mult(springing[n]);
     accel[n].add(delta[n]);
     joints[n].add(accel[n]);
    }
    point(joints[n].x, joints[n].y);
    accel[n].mult(damping[n]);
  }
}

I'm writing a program that simulates a snake, and the values of each section of the snake are stored in an array. In order to simulate the snake physics, I need to assign the subsequent section to the previous one, which is where I run into a problem.

int bar;
int foo;

void setup() {}

void draw() {
  foo = bar;
  bar = mouseX;
  println(foo);
  println(bar);
}

In this example, shouldn't foo output bar's previous mouse position? Bar is being assigned to mouseX after foo is being assigned to bar. Does the assignment operator make it so that if the variable that is assigned changes the variable being assigned to changes with it? This is very frustrating and I think the solution should be simple. How do I assign one variable to another disregarding future changes to the var being referenced?

EDIT: Here is an example of the snake simulator that should work but doesn't for the same reason, all the segments end up being the same at the end of each for loop:

int segments = int(random(3, 10));
float springing[] = new float[segments];
float damping[] = new float[segments];
PVector accel[] = new PVector[segments];
PVector[] joints = new PVector[segments];
PVector[] delta = new PVector[segments];
PVector food = new PVector(0,0,0);

void setup() {
  size(500, 500);
  stroke(255);
  for(int n = 0; n < joints.length; n++) {
    if (n == 0) joints[0] = new PVector(random(width), random(height));
    else joints[n] = joints[0];
    delta[n] = new PVector(0,0,0);
    accel[n] = new PVector(0,0,0);
    springing[n] = .05*(.07*(n+1)); 
    damping[n] = .95-(.02*n);
  }
}

void draw() {
  background(0);
  food.x =  mouseX;
  food.y = mouseY;
  for(int n = 0; n < segments; n++) {
    if (n == 0) {
     delta[0] = PVector.sub(food, joints[0]); 
     joints[0].add(delta[0]);
    }
    else {
     delta[n] = PVector.sub(joints[n-1], joints[n]);
     delta[n].mult(springing[n]);
     accel[n].add(delta[n]);
     joints[n].add(accel[n]);
    }
    point(joints[n].x, joints[n].y);
    accel[n].mult(damping[n]);
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

煮酒 2024-10-19 09:26:38

在您的示例中,foobar 是原始类型,因此结果应该符合您的期望。但是,如果它们是参考文献,那么您所写的内容将是不完整的。在这种情况下,构造函数将帮助您保留变量的旧值。

您是否尝试过在输入绘制方法时检查 bar 的值并查看分配之前和之后的实际值?

In your example, foo and bar are primitive types, so the results should match your expectations. If they were references, however, then what you have written would be incomplete. In such a case, a constructor would help you preserve the old value of the variable.

Have you tried inspecting the value of bar when you enter your draw method and seeing what it actually is before and after the assignments?

烟花易冷人易散 2024-10-19 09:26:38

draw() 循环每秒运行多次。一旦再次运行,foo 就会被 bar 的值覆盖,bar 的值包含上次执行 draw() 时的鼠标 x 位置。我认为如果将 println 语句更改为:

println("foo: " + foo + " bar: " + bar);

然后,如果您在移动鼠标时运行草图,您将看到如下条目:

foo: 76 bar: 84
foo: 84 bar: 91
foo: 91 bar: 97
foo: 97 bar: 97

正如预期的那样,每次执行 draw() 时, foo 都会采用 bar 所具有的值(即,mouseX)在上次执行draw()期间,因为您的draw()循环不断将foo设置为bar的当前值。

The draw() loop gets run many times per second. As soon as it runs again, foo gets overwritten with the value of bar, which has the mouse x position from the previous execution of draw(). I think you can see it more clearly if you change the println statements to:

println("foo: " + foo + " bar: " + bar);

Then if you run your sketch while moving the mouse you will see entries like:

foo: 76 bar: 84
foo: 84 bar: 91
foo: 91 bar: 97
foo: 97 bar: 97

As expected, each time draw() executes, foo takes on the value that bar had (i.e., mouseX) during the last time draw() executed, because your draw() loop keeps setting foo to the current value of bar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文