Ich arbeite daran, diesen freien Quellcode, den ich gefunden habe (http://solemone.de/demos/snow-effect-processing/), in eine Klasse zu verwenden, die ich in einem größeren, komplexeren "Spiel" -Code verwenden kann. Es ist ein ziemlich grundlegender Schneefall Code:Wie man den Hauptcode in eine Klasse (Schneefall) umwandelt
int quantity = 15;
float [] xPosition = new float[quantity];
float [] yPosition = new float[quantity];
int [] flakeSize = new int[quantity];
int [] direction = new int[quantity];
int minFlakeSize = 10;
int maxFlakeSize = 20;
void setup() {
size(800, 350);
frameRate(30);
noStroke();
smooth();
for(int i = 0; i < quantity; i++) {
flakeSize[i] = round(random(minFlakeSize, maxFlakeSize));
xPosition[i] = random(0, width);
yPosition[i] = random(0, height);
direction[i] = round(random(0, 1));
}
}
void draw() {
background(0);
for(int i = 0; i < xPosition.length; i++) {
ellipse(xPosition[i], yPosition[i], flakeSize[i], flakeSize[i]);
if(direction[i] == 0) {
xPosition[i] += map(flakeSize[i], minFlakeSize, maxFlakeSize, .1, .5);
} else {
xPosition[i] -= map(flakeSize[i], minFlakeSize, maxFlakeSize, .1, .5);
}
yPosition[i] += flakeSize[i] + direction[i];
if(xPosition[i] > width + flakeSize[i] || xPosition[i] < -flakeSize[i] || yPosition[i] > height + flakeSize[i]) {
xPosition[i] = random(0, width);
yPosition[i] = -flakeSize[i];
}
}
}
Ich habe ganz einfach nicht begriffen, wie das für() trennen Schleifen, ganze Zahlen und Arrays in Funktionen, die ich in eine separate Klasse setzen kann, dass ich den Titel habe Schnee, damit ich es leicht bewegen und im größeren Code manipulieren kann. Hier ist einer meiner (vielen) versucht, so weit:
Klasse Tab:
class Snow{
int quantity = 15;
float [] xPosition = new float[quantity];
float [] yPosition = new float[quantity];
int [] flakeSize = new int[quantity];
int [] direction = new int[quantity];
int minFlakeSize = 10;
int maxFlakeSize = 20;
Snow(){
frameRate(30);
noStroke();
smooth();
}
void display() {
flakeSize = round(random(minFlakeSize, maxFlakeSize));
xPosition = random(0, width);
yPosition = random(0, height);
direction = round(random(0, 1));
}
void update() {
for(int i = 0; i < xPosition.length; i++) {
ellipse(xPosition[i], yPosition[i], flakeSize[i], flakeSize[i]);
if(direction[i] == 0) {
xPosition[i] += map(flakeSize[i], minFlakeSize, maxFlakeSize, .1, .5);
} else {
xPosition[i] -= map(flakeSize[i], minFlakeSize, maxFlakeSize, .1, .5);
}
yPosition[i] += flakeSize[i] + direction[i];
if(xPosition[i] > width + flakeSize[i] || xPosition[i] < -flakeSize[i] || yPosition[i] > height + flakeSize[i]) {
xPosition[i] = random(0, width);
yPosition[i] = -flakeSize[i];
}
}
}
}
Haupt Tab:
//Ice
Snow [] flakes = new Snow[15];
int quantity = 15;
void setup(){
size(600,800);
flakes = new Snow[15];
}
void draw(){
background(0);
for(int i = 0; i < quantity; i++){
flakes[i].display();
flakes[i].update();
}
}
Ich bin sicher, es ist ein wirklich einfaches Wegbrechen des Codes nach unten und sie in die richtigen Orte zu trennen, aber ich kann es nicht begreifen. Wenn mir jemand mit diesem Prozess helfen könnte, würde ich es sehr schätzen.
Uhhh das sieht nicht wie Javascript aus ...? – Tuvia
Oh! Es tut uns leid! Es ist Java in Processing.org. Ich habe Javascript versehentlich eingefügt. – SmokeAndAsh