Hey all,
I'm working on making this bounce ball example here into a function so that I can make multiple bounce balls at once. I have it working so that I can draw multiple balls, but once I put the function together they just don't seem to want to move anywhere. I'm sure the answer is really basic so please excuse me being a noob with this
. Any suggestions?
//int size = 60; // Width of the shape
//float xpos, ypos; // Starting position of shape
//float xspeed = 2.8; // Speed of the shape
//float yspeed = 2.2; // Speed of the shape
//int xdirection = 1; // Left or Right
//int ydirection = 1; // Top to Bottom
void setup()
{
size(640, 200);
noStroke();
frameRate(30);
smooth();
// Set the starting position of the shape
//xpos = width/2;
//ypos = height/2;
}
void draw()
{
background(102);
ballMaker(100, 11, 2.8, 2.2, 1, 1, 50);
ballMaker(12, 11, 2.8, 2.2, 1, 1, 50);
ballMaker(67, 146, 2.8, 2.2, 1, 1, 50);
}
void ballMaker(float xpos, float ypos, float xspeed, float yspeed, int xdirection, int ydirection, int size)
{
// Update the position of the shape
xpos = xpos + ( xspeed * xdirection );
ypos = ypos + ( yspeed * ydirection );
// Test to see if the shape exceeds the boundaries of the screen
// If it does, reverse its direction by multiplying by -1
if (xpos > width-size || xpos < 0) {
xdirection *= -1;
}
if (ypos > height-size || ypos < 0) {
ydirection *= -1;
}
// Draw the shape
ellipse(xpos+size/2, ypos+size/2, size, size);
}
Thanks!
I'm working on making this bounce ball example here into a function so that I can make multiple bounce balls at once. I have it working so that I can draw multiple balls, but once I put the function together they just don't seem to want to move anywhere. I'm sure the answer is really basic so please excuse me being a noob with this
. Any suggestions?
//int size = 60; // Width of the shape
//float xpos, ypos; // Starting position of shape
//float xspeed = 2.8; // Speed of the shape
//float yspeed = 2.2; // Speed of the shape
//int xdirection = 1; // Left or Right
//int ydirection = 1; // Top to Bottom
void setup()
{
size(640, 200);
noStroke();
frameRate(30);
smooth();
// Set the starting position of the shape
//xpos = width/2;
//ypos = height/2;
}
void draw()
{
background(102);
ballMaker(100, 11, 2.8, 2.2, 1, 1, 50);
ballMaker(12, 11, 2.8, 2.2, 1, 1, 50);
ballMaker(67, 146, 2.8, 2.2, 1, 1, 50);
}
void ballMaker(float xpos, float ypos, float xspeed, float yspeed, int xdirection, int ydirection, int size)
{
// Update the position of the shape
xpos = xpos + ( xspeed * xdirection );
ypos = ypos + ( yspeed * ydirection );
// Test to see if the shape exceeds the boundaries of the screen
// If it does, reverse its direction by multiplying by -1
if (xpos > width-size || xpos < 0) {
xdirection *= -1;
}
if (ypos > height-size || ypos < 0) {
ydirection *= -1;
}
// Draw the shape
ellipse(xpos+size/2, ypos+size/2, size, size);
}
Thanks!
1
