Classes, objects and animation
in
Programming Questions
•
1 year ago
Hey all, I've been working on this program for a few days now and i cant seem to get the animation aspect of it to work.. Any help would be greatly appreciated
------MAIN TAB-----
import javax.swing.JOptionPane;
int numSmileys;
int noOfMousePresses = 0;
int maxCircles;
boolean moving = false;
Smiley blueSmiley = new Smiley();
// add any method you define here
//setup
void setup() {
background(255);
size(500, 500);
smooth();
do {
String input = JOptionPane.showInputDialog("Enter number of Smileys (4-8)");
numSmileys = Integer.parseInt(input);
}
while (numSmileys < 4 || numSmileys > 8);
}
//draw
void draw() {
//background(255);
if (noOfMousePresses > numSmileys)
blueSmiley.move();
}
//any other mouse / key event methods
void mousePressed() {
noOfMousePresses++;
if (noOfMousePresses <= numSmileys) {
blueSmiley.display();
}
}
------Class Tab-----
class Smiley {
float speedX, speedY;
float x, y, dia, eye, mouth;
float moveX, moveY;
boolean alive = false;
boolean moving = false;
//constructor, with values set
Smiley() {
// values of data members
dia = 60;
eye = 10;
mouth = 15;
speedX = (int)random(1, 4);
speedY = (int)random(1, 4);
}
void display() {
x=mouseX;
y=mouseY;
stroke(1);
fill(255);
ellipse (x, y, dia, dia);
fill(0);
line(x-mouth, y+eye, x+mouth, y+eye);
fill(0, 0, 255);
noStroke();
ellipse (x-mouth, y-eye, eye, eye);
ellipse (x+mouth, y-eye, eye, eye);
}
void setAlive() {
alive = true;
}
void setMoving() {
moving = true;
}
void move() { //actual moving code goes here
blueSmiley.setMoving();
x+=speedX;
y+=speedY;
print("Working!");
//smiley bounces off walls
if (x>width-dia/2 || x<dia/2) {
speedX = speedX*-1;
}
if (y>height-dia/2 || y<dia/2) {
speedY = speedY*-1;
}
}
}
So basically what im trying to do is to have a user input a number (4-8). Then with that number create that many 'smileys'. Once the mouse has been pressed AFTER all the smileys have been generated. I want them to move in random directions, and bounce of walls. Eventually i would like to code them to ensure they collide.
For example, user inputs '4', the first 4 mouse presses will generate 4 smileys, the 5th mouse press will cause them to move.
If anyone would like any extra information to help me solve this, do not hesitate to ask :D.
Thank you for your time.
1