We are about to switch to a new forum software. Until then we have removed the registration on this forum.
How would I incorporate a changing shape command in a sketch which that I have a class in. I want to change the circles to a rectangle every 3 seconds. Sorry this is like my 3rd forum regarding the changing of shapes.
Below is my code:
/* This sketch is based one the Bounce example
*
* When the shape hits the edge of the window, it reverses its direction.
*/
/*Ball b = new Ball(); //creates an object from Ball
Ball c= new Ball(); //creates an object from Ball
Ball d= new Ball(); //creates an object from Ball*/
int numberOfBalls = 2; //this variable controls how many Balls
Ball[] bob = new Ball[numberOfBalls]; //this creates an array of Balls
//make an array list with a flexible number of objects in it
ArrayList<Ball> bobslist = new ArrayList<Ball>();
//FRAME COUNT (TIMER)
float shape;
if (frameCount%120==0) { //code runs every 2 seconds
shape=!shape;
void setup()
{
size(800, 500);
noStroke();
frameRate(30);
ellipseMode(RADIUS);
// Set the starting position of the shape
//loop to create 10 (numberOfBalls) Ball objects in the Ball array
for ( int i=0; i<numberOfBalls; i=i+1) {
bob[i] = new Ball(width/2, height/2); //this creates Ball object in the array spaces from 0 to 1 to 2 to....numberOfBalls
}
}
void draw()
{
background(#6593B9);
/* b.move(); //this is one ball only - I want lots!
b.display(); */ //the */ is used to comment long lines of code
for ( int i=0; i<numberOfBalls; i=i+1) {
bob[i].move(); //this will move each Ball object in the array
bob[i].display(); //this will display each Ball object
}
// move and display all the Balls in the balls list
for ( int i=0; i<bobslist.size(); i=i+1) {
Ball temp = bobslist.get(i); //this gets the ith object in the array list
temp.display();
temp.move();
}
}
//this method runs every time the mouse is clicked
void mousePressed() {
//loop to create 10 (numberOfBalls) Ball objects in the Ball array
for ( int i=0; i<5; i=i+1) {
// bob[i] = new Ball(mouseX, mouseY, width, height); //this creates Ball object in the array spaces from 0 to 1 to 2 to....numberOfBalls
//add one ball object to the bobslist arraylist
bobslist.add(new Ball (mouseX, mouseY)); //.add means run the add method for arraylists
}
}
//this runs whenever any key is pressed
void keyPressed() {
for( int i=0; i<5; i=i+1)
if(bobslist.size()>0){
bobslist.remove(0);
}
}
And below is my class:
//this class defines how balls look and move
public class Ball {
//all the code in here defines the Ball objects
//first, declare any class variables
int rad; // Width of the shape
float xpos, ypos; // Starting position of shape
float xspeed; // Speed of the shape
float yspeed; // Speed of the shape
int xdirection; // Left or Right
int ydirection; // Top to Bottom
float redcolor; // Declare 3 variables for ball color
float greencolor;
float bluecolor;
//a constructor: code that runs when we make an object
public Ball() {
rad = (int)random(100); //This is to randomize the size of the balls without the balls flickering
xspeed = random(10); //random speed for balls
yspeed = random(10);
xdirection = 1;
ydirection = 1;
xpos = random(rad, 400-rad);
ypos = random(rad, 200-rad);
redcolor = random(255); //pick random color value
greencolor = random(255);
bluecolor = random(255);
}
//an overload constructor with width and height
public Ball(int w, int h) { //w=width, h=height
rad = (int)random(100); //This is to randomize the size of the balls without the balls flickering
xspeed = random(10); //random speed for balls
yspeed = random(10);
xdirection = 1;
ydirection = 1;
xpos = random(rad, w-rad);
ypos = random(rad, h-rad);
redcolor = random(255); //pick random color value
greencolor = random(255);
bluecolor = random(255);
}
//a overloaded constructor: takes 2 location values
public Ball(int xposition, int yposition, int w, int h) { //parameters
rad = (int)random(100); //This is to randomize the size of the balls without the balls flickering
xspeed = random(10); //random speed for balls
yspeed = random(10);
float change = random(-100, 100);
if (change < 0) {
xdirection = -1;
} else {
xdirection = 1;
}
change = random(-100, 100);
if (change < 0) {
ydirection = -1;
} else {
ydirection = 1;
}
xpos = xposition; //object will start in the parameter location
ypos = yposition;
redcolor = random(255); //pick random color value
greencolor = random(255);
bluecolor = random(255);
}
//methods to control the Ball objects
public void move() {
// Update the position of the shape
xpos = xpos + ( xspeed * xdirection );
ypos = ypos + ( yspeed * ydirection );
bounce();
}
public void bounce() {
// 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-rad || xpos < rad) {
xdirection *= -1;
}
if (ypos > height-rad || ypos < rad) {
ydirection *= -1;
}
}
public void display() {
// Draw the shape
ellipse(xpos, ypos, rad, rad);
fill(redcolor, greencolor, bluecolor);
}
}
//METHOD
void changeShapes () {
if (shape) {
ellipse(xpos, ypos, rad, rad);
fill(redcolor, greencolor, bluecolor);
} else {
rect(xpos, ypos, rad, rad);
fill(redcolor, greencolor, bluecolor);
}
}
Can you guys spot any errors in code?
Answers
https://forum.processing.org/two/discussion/comment/102633/#Comment_102633
Kf