Hi,
1) "mousePressed()" is just called everytime you press a mouse. I think it's easier to handle mouse-clicks with the build-in function
mousePressed() an
mouseReleased()
2) i don't know if my code runs faster than yours. But if you rely on a good timing, then you should not animate things frame-based. If you increase variables on each frame, then the code might run slower, if your computer slow. You could calculate values depending on tim (see
millis()). But for the start, it might be easier to use frames.
3) Right. I didn't know what you had in mind, thought you wanted to disable mouse-clicks once leafs are displayed. To solve this, you could set the state to "0" as soon as the mouse is released.
4) the code from setup(9 should stay inside setup(). The code from the letters example could be moved to the fallingLetters()-function
5) then just display the leafs, and then display the letters over them.
Here is an updated example, i changed several things to make it more logical to me.
We still have three states:
- fading out the leafs
- fading in the leafs
- show the letters
When the mouse is pressed state is 1. When the mouse is released, the state = 0.
When mouse is pressed long enough (600 frames), then the letters begin to fall and mouse-interaction is ignored.
After all letters are fallen, everything is reset.
- int xPosition = 50;
int yPosition;
int xOffset = 15;
boolean lettersFalling = false;
String word = "processing";
ArrayList<Letter> letters;
PImage img;
int n, r, g, b=2, a, x, y; // i changed some names
int state = 0; // stores the current phase (0=fadeOut, 1=fadeIn, 2=letters)
int leafTime = 0;
int letterTime;
void setup () {
size(800, 500);
img = loadImage("Leaf.gif");
yPosition = height/2;
letters = new ArrayList<Letter>();
letters.add(new Letter(word.charAt(0), xPosition, yPosition));
}
void draw() {
println(state);
background (0);
// check in which phase you are and display stuff accordingly
switch(state) {
case 0:
if (leafTime>0)
leafTime--;
showLeaf();
break;
case 1:
if (leafTime<600)
leafTime++;
else {
state =2;
lettersFalling = true;
}
showLeaf();
break;
case 2:
showLeaf();
fallingLetters();
// letters are fallen: reset variables and and return to state 0
if (!lettersFalling) {
letterTime =400;
leafTime =0;
letters = new ArrayList<Letter>();
letters.add(new Letter(word.charAt(0), xPosition, yPosition));
state = 0;
}
break;
}
}
void showLeaf() {
// increase transparency
a = min(leafTime, 255);
// increase red-value after 150 frames
if (leafTime > 150 && leafTime <=300) {
r = min(26 + (leafTime-150), 255);
}
// decrease green-value after 300 frames
else if (leafTime<=500) {
g = max(214 + 2*(300-leafTime), 0);
}
// number of leafs
if (leafTime<600)
n =1;
else
n =13;
// display the leafs
for (int i=0;i<n;i++) {
// set position
if (leafTime<600) {
x = (int)random(150, 160);
y = (int)random(10, 20);
tint (r, g, b, a);
}
else {
x = (int)random(-400, 800);
y = (int)random(-400, 500);
tint(random(0, 255), random(0, 255), random(0, 255));
}
image(img, x, y);
}
}
void fallingLetters() {
// do the falling-letters-stuff here
int numLetters = letters.size();
// check if the last letter has already reached its end-position
Letter last = letters.get(numLetters-1);
if (last.maxY > last.y) {
last.update();
}
// .. otherwise create the next letter and add it to your ArrayList
else if (numLetters < word.length()) {
letters.add(new Letter(word.charAt(numLetters), xPosition+xOffset*numLetters, yPosition));
}
else {
lettersFalling =false;
}
// show all letters that you have so far
for (int i =0; i< numLetters; i++) {
Letter current = letters.get(i);
current.display();
}
}
void mousePressed() {
if (state == 0) {
state = 1;
}
}
void mouseReleased() {
if (state == 1) {
state = 0;
}
}
class Letter {
float speed = 3;
float offset = 100;
char c;
float x, y, maxY;
Letter(char pC, float pX, float pY) {
x = pX;
y = pY-offset;
c = pC;
maxY = pY;
}
void update() {
if (y+speed <= maxY ) {
y+= speed;
}
else {
y = maxY;
}
}
void display() {
text(c, x, y);
}
}
Depending on what you have in mind, there might be better ways to solve, but maybe this helps.
Just tinker around with it, untill you are happy.