我是 processing/java/code 的新手,但希望對我的草圖有所幫助。
我正在嘗試創建一個看起來像墨水的草圖,顯示字母/字符,然后淡出而不是粒子本身。受https://openprocessing.org/sketch/1576908的啟發,我在整個粒子建構式中遇到了錯誤,并且在 void update(p) 行出現錯誤:
//update the velocity and location of particle
void update(p){
this.acceleration.add(createVector((noise(this.location.x)*2-1), (noise(this.location.y)*2-1)));
this.velocity.add(this.acceleration);
this.acceleration.set(0,0);
this.location.add(this.velocity);
this.alpha -= this.rate ;
// here is the recursion condition
if(this.alpha<=this.palpha*0.25 && this.palpha>10) {
p.push(new particle(this.location.x, this.location.y, this.rate*0.25, this.palpha*0.5));
}
}
這是我的完整代碼
謝謝!
String[] particles = {"a", "b", "c", "d"} ; //string of particles
int velocity;
int acceleration;
int location;
int alpha;
int p;
void setup() {
size(600, 600);
background(255);
}
void draw() {
if(mousePressed) {
// spawn a new particle and add it to the array
particles.push(text(particles, mouseX, mouseY, 75));
textSize(random(20, 40));
}
// update and show the particles
for(int i=particles.length-2; i>=0; i--) {
particles[i].update(particles);
particles[i].show();
if(particles[i].alpha<=2) particles.splice(i, 5); // remove the dead particle
}
}
//particle class
class particle{
//constructor called when creating an instance of this class
// x & y are the location, r is the rate of decay, a is the starting alpha value
particle(float x, float y, float r, float a){
this.location = createVector(x,y) ;
this.velocity = createVector(random(-1,1),random(-1,1));
this.acceleration = createVector();
this.alpha = this.palpha=a ;
this.amp=4; // size of the particle
this.rate = r;
}
//update the velocity and location of particle
void update(p){
this.acceleration.add(createVector((noise(this.location.x)*2-1), (noise(this.location.y)*2-1)));
this.velocity.add(this.acceleration);
this.acceleration.set(0,0);
this.location.add(this.velocity);
this.alpha -= this.rate ;
// here is the recursion condition
if(this.alpha<=this.palpha*0.25 && this.palpha>10) {
p.push(new particle(this.location.x, this.location.y, this.rate*0.25, this.palpha*0.5));
}
}
//show the particles
void show(){
noStroke() ;
fill(0,35,25, this.alpha) ;
ellipse(this.location.x, this.location.y, this.amp);
}
} // end particle class```
uj5u.com熱心網友回復:
您在這里至少有兩個單獨的問題:
- 如何將 p5.js 草圖移植到處理?
- 如何為每個粒子添加文本?
將來,我建議將問題分解為可以獨立解決的更簡單/更短的問題。
讓我們看看處理呈現的語法錯誤:
1.
particles.push(text(particles, mouseX, mouseY, 75));
錯誤與
The function "text()" expects parameters like: "text(int, float, float, float)"
這里的問題被稍微掩蓋了。看起來不是呼叫text(yourTextString, yourTextX, yourTextY);你有不同的引數。實際上這里有兩個問題:
push()是 JavaScript Array 的函式。您需要ArrayListadd()在 Processing (Java) 中使用 an及其方法。- 目前粒子類不處理文本。您可以添加一個
String text屬性,您可以使用修改后Particle(float x, float y, float r, float a, String text)的建構式提供該屬性:(并且您將建構式引數分配給實體屬性(例如this.text = textl)
createVector存在于 p5.js 中。在處理中,您可以將其切換為new PVector(). 此外,您需要將在建構式中初始化的變數宣告為類的一部分(例如location,velocity,acceleration,alpha,palpha,amp,rate)。ellipse(this.location.x, this.location.y, this.amp);缺少最后一個引數:ellipse(this.location.x, this.location.y, this.amp, this.amp);
這是應用了上述注釋的代碼的修改版本:
// original sketch by OpenProcessing user Prasad
// https://openprocessing.org/sketch/1576908
String[] particlesText = {"a", "b", "c", "d"} ; //string of text
// array of particles
ArrayList<Particle> particles = new ArrayList<Particle>();
int velocity;
int acceleration;
int location;
int alpha;
int p;
void setup() {
size(600, 600);
background(255);
}
void draw() {
if(mousePressed) {
// spawn a new particle and add it to the array
// use % to loop over text (e.g .a,b,c,d,a...etc)
int textIndex = particles.size() % particlesText.length;
// grab the text from the array
String text = particlesText[textIndex];
// add a new particle providing text as well
particles.add(new Particle((float)mouseX, (float)mouseY,5.0, 75.0, text));
textSize(random(20, 40));
}
// update and show the particles
for(int i=particles.size()-2; i>=0; i--) {
Particle particle = particles.get(i);
particle.update(particles);
particle.show();
if(particle.alpha<=2) particles.remove(i); // remove the dead particle
}
}
//particle class
class Particle{
PVector location;
PVector velocity;
PVector acceleration;
float alpha;
float palpha;
float amp;
float rate;
String text = "";
//constructor called when creating an instance of this class
// x & y are the location, r is the rate of decay, a is the starting alpha value
Particle(float x, float y, float r, float a, String text){
this.location = new PVector(x,y) ;
this.velocity = new PVector(random(-1,1),random(-1,1));
this.acceleration = new PVector();
this.alpha = this.palpha=a ;
this.amp=4; // size of the particle
this.rate = r;
this.text = text;
}
//update the velocity and location of particle
void update(ArrayList<Particle> p){
this.acceleration.add(new PVector((noise(this.location.x)*2-1), (noise(this.location.y)*2-1)));
this.velocity.add(this.acceleration);
this.acceleration.set(0,0);
this.location.add(this.velocity);
this.alpha -= this.rate ;
// here is the recursion condition
if(this.alpha<=this.palpha*0.25 && this.palpha>10) {
p.add(new Particle(this.location.x, this.location.y, this.rate*0.25, this.palpha*0.5, this.text));
}
}
//show the particles
void show(){
noStroke() ;
fill(0,35,25, this.alpha);
//render the ellipse
ellipse(this.location.x, this.location.y, this.amp, this.amp);
// render the text
textSize(this.amp * 6);
text(this.text, this.location.x, this.location.y);
}
} // end particle class
(請注意,您可以選擇不渲染省略號,并且可以將文本大小調整為更美觀的東西。此外,當您使用其他人的代碼時,請始終記下他們。)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/520248.html
標籤:细绳班级全局变量加工
