问题
My question concerns creating an array of moving objects. The goal is to have each created "symbol" starts at x=200 when created and then moves along the x-axis. This is possible by creating individual objects, though when I try to make them appear in an array they just appear on top of each other (made the balls transparent so you can see them stack). Is there a way to create an array of individual array objects so that this doesn't happen.
int numSymbols = 100;
int symbolXPos;
int i;
Symbol[] symbols = new Symbol[numSymbols];
void setup(){
for(int i = 0; i < numSymbols; i++){
symbols[i] = new Symbol();
}
size(748,1024);
}
void draw(){
background(255,255,255);
i++;
if(i > 10){
symbols[1].addSymbol(i);
}
if(i > 100){
symbols[2].addSymbol(i);
}
if(i > 200){
symbols[3].addSymbol(i);
}
}
int symbolY = 748-2*6-89-180;
int symbolX = 200;
int symbolPos;
boolean firstLoop = true;
int xSpeed;
class Symbol{
void addSymbol(int xSpeed){
if(firstLoop == true){
symbolXPos = xSpeed;
firstLoop = false;
}else{
xSpeed = symbolX + (xSpeed - symbolXPos);
fill(0,0,0,20);
noStroke();
rect(xSpeed,symbolY, 36, 36, 18);
}
}
}
回答1:
The first part that looks strange is that you have these properties that seem related to a Symbol instance, and not to the main scope:
int symbolY = 748-2*6-89-180;
int symbolX = 200;
int symbolPos;
boolean firstLoop = true;
int xSpeed;
perhaps you meant something like this:
int numSymbols = 100;
int symbolXPos;
int i;
Symbol[] symbols = new Symbol[numSymbols];
void setup() {
for (int i = 0; i < numSymbols; i++) {
symbols[i] = new Symbol();
}
size(748, 1024);
}
void draw() {
background(255, 255, 255);
i++;
if (i > 10) {
symbols[1].addSymbol(i);
}
if (i > 100) {
symbols[2].iaddSymbol(i);
}
if (i > 200) {
symbols[3].addSymbol(i);
}
}
class Symbol {
int symbolY = 748-2*6-89-180;
int symbolX = 200;
int symbolPos;
boolean firstLoop = true;
int xSpeed;
void addSymbol(int xSpeed) {
if (firstLoop == true) {
symbolXPos = xSpeed;
firstLoop = false;
} else {
xSpeed = symbolX + (xSpeed - symbolXPos);
fill(0, 0, 0, 20);
noStroke();
rect(xSpeed, symbolY, 36, 36, 18);
}
}
}
Even so, it feels like the code is more complex than it should be and there is some confusion regarding classes and objects/instances.
Let's assume you're simply going to move symbols horizontally. You could use a class as simple as this:
class Symbol{
//position
float x;
float y;
//speed
float speedX;
void updateAndDraw(){
//update position
x += speedX;
//reset to 0 if out of bounds
if(x > width) x = 0;
//draw
rect(x, y, 8, 8, 18);
}
}
It has an x,y and just a speedX
for now. The x property is incremented by the speed, and the updated position is used to render the symbol on screen.
Here's a basic sketch using the above class:
int numSymbols = 100;
Symbol[] symbols = new Symbol[numSymbols];
void setup(){
size(748, 1024);
fill(0, 0, 0, 64);
noStroke();
for (int i = 0; i < numSymbols; i++) {
symbols[i] = new Symbol();
//use different speeds to easily tell the symbols appart
symbols[i].speedX = 0.01 * (i+1);
//increment y position so symbols don't overlap
symbols[i].y = 10 * i;
}
}
void draw(){
background(255);
for (int i = 0; i < numSymbols; i++) {
symbols[i].updateAndDraw();
}
}
class Symbol{
//position
float x;
float y;
//speed
float speedX;
void updateAndDraw(){
//update position
x += speedX;
//reset to 0 if out of bounds
if(x > width) x = 0;
//draw
rect(x, y, 8, 8, 18);
}
}
The key points are:
- using properties related to a Symbol within the class, not outside (which would mean the global scope)
- using different positions so Symbol instances don't overlap
Be sure to check out Daniel Shiffman's Objects tutorial, it provides a nice and easy intro to Object Oriented Programming
来源:https://stackoverflow.com/questions/36330449/array-of-moving-objects-stacking-up