gpt4 book ai didi

java - 调整在Processing中的setup()中创建的对象

转载 作者:行者123 更新时间:2023-11-30 07:37:42 29 4
gpt4 key购买 nike

我是处理新手,我正在尝试使用调整工具。我的问题是,我似乎无法调整 setup() 方法中创建的变量的值。

下面是一个示例:

Ellipse e;

void setup(){
size(640, 480);
e = new Ellipse();
}

void draw(){
background(220);
e.draw();
}

class Ellipse {
float w, l;

Ellipse(){
this.w = 300.0;
this.l = 300.0;
}

void draw(){
ellipse(300, 300 , w, l);
}
}

当我使用“Tweak”时,我可以调整 Ellipse 类中 300 的所有四个实例。它的工作原理与第三个和第四个的预期完全一样,但是当我调整 wl 的值时,什么也没有发生。

我认为这是因为 Ellipse 对象是通过 setup() 而不是 draw() 创建的。这是正确的诊断吗?有解决问题的方法吗? (我不想在每次调用 draw() 时创建一个全新的对象)。

最佳答案

您将看到的唯一变化是当您更改尚未发生的数字时。

您的Ellipse 已经构造完毕,因此在构造函数中更改300 的值不会执行任何操作。请注意,如果您要创建 Ellipse实例,那么该新实例确实会获取更改。

您可以调整前两个 300 值,因为它们每帧都会“发生”。

如果您希望能够调整 wl 变量,您需要将它们放在每帧都会发生的位置。你可以这样做:

  void draw(){
w = 300;
l = 300;
ellipse(300, 300 , w, l);
}

或者您可以将其缩短为:

  void draw(){
ellipse(300, 300 , 300, 300);
}

来自TweakMode homepage :

Scope of change

Without going into too much detail of how tweak mode operates internally, you should remember that changing a number will have effect only if the tweaked code will be executed in the future. For example, take the following code:

void setup()
{
size(200, 200);
background(20);
}

void draw()
{
fill(100, 0, 0);
ellipse(100, 100, 50, 50);
}

In this code, changing the values of 'size(...)' and 'background(...)' functions will have no effect while the sketch is running because these lines of code happened once on setup and will never be executed again during the sketch life. On the contrary, changing the values of fill() and ellipse() will have effect because 'draw' is executed over and over again.

The same principle applies to global variables, for example:

int CIRCLE_SIZE = 50;

void setup()
{
size(200, 200);
background(20);
}

void draw()
{
int red = 100;
fill(red, 0, 0);
ellipse(100, 100, CIRCLE_SIZE, CIRCLE_SIZE);
}

In this case, changing the value assigned to CIRCLE_SIZE (50) will have no effect because the assignment of the value '50' to the variable CIRCLE_SIZE happened only once during the sketch life, so the value of CIRCLE_SIZE will remain the same even if the assigned value is changed. On the contrary, changing the assigned value of the variable 'red' (100) will have the expected effect because this assignment happens every draw.

A simple solution to the problem with the global variables is to reassign values you wish to tweak in the draw function. For example, the following draw function will elliminate the issue we had with the size of the ellipse in the previous code.

void draw()
{
CIRCLE_SIZE = 50;
int red = 100;
fill(red, 0, 0);
ellipse(100, 100, CIRCLE_SIZE, CIRCLE_SIZE);
}

Adding the line 'CIRCLE_SIZE = 50;' to the top of the draw function made it possible to tweak this number and control also the size of the circle in real time.

关于java - 调整在Processing中的setup()中创建的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35158379/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com