如何解决导入PApplet时如何使用对象构造函数调用display()方法在visual studio代码上使用java进行处理
我想弄清楚如何在 draw 方法中使用对象来调用方法,例如 display()、setup()。我正在使用带有导入的处理包的 Visual Studio 代码,并且基本上使用 java 来完成该过程。我也在关注 daniel shiffman 的代码性质教程,发现自己遇到了麻烦并且无法正常工作。我可以通过调用“display();”来让它工作在 draw 方法中,但我想知道如何使用对象“w.display()”。
我的代码是这样的:
导入 processing.core.papplet;
class RandomWalker 扩展 papplet {
// walker only needs two pieces of data
// x location
int x;
// y-location
int y;
//global var
RandomWalker w;
public void settings(){
//establishing the size of the window
size(640,360);
}//end method
//constructor for the walker class and its objects
RandomWalker(){
//here we initalise the walkers objects and set the starting location
// starting point - centre of window
x = width /2;
y = height/2;
}//end constructor
// a walker has two functions. 1. the walker will display itself as a white dot
public void display(){
stroke(0);
point(x,y);
}//end method
// directs walker to take a step
public void step(){
//picks a random floating point number between 0 - 4
int choice = (int)(random(4)); // chooses between 0,1,2,3
if( choice == 0){
x++; // move right
// println("working");
}//end if
else if(choice == 1) {
x--; //move left
}//end else if
else if(choice ==2){
y++; // move up
}//end else if
else {
y--; //move down
}//end else
}//endmethod
//creating the setup method
public void setup(){
//creating an object by calling the constructor with the new operator
w = new RandomWalker(); // creating the walker
background(255);
// frameRate(190);
}//end method
//in this method,we ask the walker to take a step and draw a dot
public void draw(){
w.step();
w.display();
}//end method
}//结束类
解决方法
这里的问题是您需要 draw()
方法才能做您想做的事。您可能知道,draw()
方法是 Processing(或在 PApplet 的情况下)使用的主循环,我没有查看引擎盖下的细节,但我已经根据经验知道还有更多内容不如直视。
因此,您的忍者修复 - 从 PApplet 的 display()
方法中调用 draw()
方法 - 实际上是正确的方法。
如果你只想要一帧,setup()
方法会很好,但我猜你会想要循环这个并让它随着时间的推移而发展,所以请继续使用draw()
方法。这不是黑客,这是要走的路!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。