expanding and adding to Array when working with Classes
in
Programming Questions
•
11 months ago
Hi,
I've been trying to solve this for a few days and can't seem to so I figured I'd come and ask to see if anyone knew where I was going wrong and what I should be doing.
Basically I'm creating an array of Objects, which are balls that travel back and forth on the screen. I've added a random number feature in the code and when the random number generated equals 3 then I want to update the array of objects, expanding it by one and adding a new ball into the Array and then onto the screen.
My current main script looks like this:
my Class for the Ball looks like this:
When I initially tried to solve this problem I figured I'd have to reiterate the for loop in setup every time I wanted to lengthen the array. Then I realised that 1. this was a bad idea because its inefficient & I'd loose the location of the balls on screen and 2. a more sensible thing to do would be to simply extend the size of the Array by one and then stick a new ball object in the new empty slot. I try to do this in the main code with the following code inside the if statement in void loop:
but when I try to do this I get an error telling me I can't convert from Object to addOneBall.Ball[]; I don't fully understand the error message. I've took the syntax of the code from the example given in the processing reference for the expand() method which is available here: http://www.processing.org/reference/splice_.html
and the expand() method which is available here: file:///Applications/Processing.app/Contents/Resources/Java/modes/java/reference/expand_.html
Can someone tell me how to make my code work in the way I want it and where I'm going wrong in terms of how I'm approaching this problem?
Thanks for reading
Bunzy
I've been trying to solve this for a few days and can't seem to so I figured I'd come and ask to see if anyone knew where I was going wrong and what I should be doing.
Basically I'm creating an array of Objects, which are balls that travel back and forth on the screen. I've added a random number feature in the code and when the random number generated equals 3 then I want to update the array of objects, expanding it by one and adding a new ball into the Array and then onto the screen.
My current main script looks like this:
int numBalls = 8; // variable to hold the initial size of array
Ball[] balls = new Ball[numBalls]; // array to hold ball objects
Ball addOneBall;
void setup()
{
// set up screen
size(500, 500);
smooth();
noStroke();
// for loop to fill array with balls
for (int i = 0; i < balls.length; i++)
{
balls[i] = new Ball();
}
}
void draw()
{
fill(255, 200); //white see-through background
rect(0, 0, width, height); // draw rect instead of background so we can use alpha to give comet tail effect on balls
int addOne = int(random(100)); //generate a random number to use in a conditional in a moment
println("random number is " + addOne); //print random number to terminal to monitor
//conditional statement if random number is three...
if (addOne == 3)
{
int currentLength = balls.length; // Create and int of the value of the current length of the array
println("Current length of balls array is " + currentLength); ///print value to terminal
balls = (Ball[]) expand(currentLength+1); //extend the current array by one
addOneBall = new Ball(); // Create new Ball
balls = splice(balls, addOneBall, currentLength); //splice addOneBall and balls array, placing addOneBall in the last position of the array
}
//
for (int i = 0; i < balls.length; i++)
{
balls[i].move();
balls[i].updateMe();
balls[i].display();
}
}
my Class for the Ball looks like this:
- class Ball
{
float x, y;
float diam;
float radius;
float speed;
int dir =1;
int decision = int(random(-1, 1));
color shade;
int addABall = 1;
Ball()
{
x = random(width);
y = random(height);
diam = (random(10)+10);
radius = diam/2;
speed = random(5);
}
void move()
{
x+=(speed * dir);
if ((x > width) || (x< 0))
{
dir *= -1;
}
}
void updateMe()
{
boolean touching = false;
for (int i = 0; i< balls.length; i++)
{
Ball otherBall = balls[i];
if (otherBall != this)
{
float dis = dist(x, y, otherBall.x, otherBall.y);
if ((dis - radius - otherBall.radius) < 0)
{
touching = true;
break;
}
}
}
if (touching)
{
shade = color(200);
display();
}
else
{
shade = color(0);
display();
}
}
void display()
{
fill(shade);
ellipse(x, y, diam, diam);
}
}
When I initially tried to solve this problem I figured I'd have to reiterate the for loop in setup every time I wanted to lengthen the array. Then I realised that 1. this was a bad idea because its inefficient & I'd loose the location of the balls on screen and 2. a more sensible thing to do would be to simply extend the size of the Array by one and then stick a new ball object in the new empty slot. I try to do this in the main code with the following code inside the if statement in void loop:
- int currentLength = balls.length; // Create an int of the value of the current length of the array
println("Current length of balls array is " + currentLength); ///print value to terminal
balls = (Ball[]) expand(currentLength+1); //extend the current array by one
addOneBall = new Ball(); // Create new Ball
balls = splice(balls, addOneBall, currentLength); //splice addOneBall and balls array, placing addOneBall in the last position of the array
but when I try to do this I get an error telling me I can't convert from Object to addOneBall.Ball[]; I don't fully understand the error message. I've took the syntax of the code from the example given in the processing reference for the expand() method which is available here: http://www.processing.org/reference/splice_.html
and the expand() method which is available here: file:///Applications/Processing.app/Contents/Resources/Java/modes/java/reference/expand_.html
Can someone tell me how to make my code work in the way I want it and where I'm going wrong in terms of how I'm approaching this problem?
Thanks for reading
Bunzy
1