Arrays and getting more than one ball to spawn.
in
Programming Questions
•
2 years ago
OK so i was wondering how to get more than one ball to spawn using arrays. I still don't fully grasp the concept of arrays at the moment and i want to be able to understand them.
here is my code for the moment.
float ball[];
float mx;
float my;
int radius = 0;
int edge = 56;
int inner = edge + radius;
float directionx = 1;
float directiony = 1;
int speed = 3;
//Background colour variables
float a = 0;
float b = 0;
float c = 0;
float d = 102;
float e = 204;
float f = 255;
void setup() {
size(750, 750);
noStroke();
smooth();
ellipseMode(RADIUS);
rectMode(CORNERS);
ball = new float[12];
for(int i = 0;i<12; i = i+1) {
ball [i] = 1;
}
}
void background1()
{
background(color(d+a,e+b,f+c));
//Change the background colours
a = (random(-1,1)) + a;
b = (random(-1,1)) + b;
c = (random(-1,1)) + c;
}
void draw() {
background1();
if(mousePressed) {
mx = mouseX;
my = mouseY;
}
mx = constrain(mx, inner+24, width - inner-24);
my = constrain(my, inner+24, height - inner-24);
//control for left and right border
if(mx > 669) {
directionx = -speed;
}
if(mx < 81) {
directionx = speed;
}
//control for bottom and top border
if(my > 669) {
directiony = -speed;
}
if(my < 81) {
directiony = speed;
}
noFill();
stroke(0);
strokeWeight(2);
rect(edge, edge, width-edge, height-edge);
fill(255);
if(mouseButton == LEFT)
{
noFill();
if(radius<24) {
radius=radius+3;
}
ellipse(mx, my, radius, radius);
mx = mx + directionx;
my = my + directiony;
}
}
1