How to optimize this code ?
in
Programming Questions
•
2 years ago
Hi,
this program display a random text from an array each time the user clicks on the mouse. I used a class (class Phrase) to create many instances. After a while (I used a timer), the texts disappear with a slight fade out effect.
The problem is when you click fast and when there is many instances on screen, the application slow down until there is no more texts.
Is there a way to optimize the code and prevent it to slow down the app ? Because I project to display other graphics and effects, and I'm afraid the things will get worse and worse...
Perhaps it would be possible to kill the instances when they have completly faded out, but I don't know how to do it.
Here is the code :
this program display a random text from an array each time the user clicks on the mouse. I used a class (class Phrase) to create many instances. After a while (I used a timer), the texts disappear with a slight fade out effect.
The problem is when you click fast and when there is many instances on screen, the application slow down until there is no more texts.
Is there a way to optimize the code and prevent it to slow down the app ? Because I project to display other graphics and effects, and I'm afraid the things will get worse and worse...
Perhaps it would be possible to kill the instances when they have completly faded out, but I don't know how to do it.
Here is the code :
- PFont font;
- String [] vers;
- ArrayList plsphrases;
- int taillevers; // pour verifier le nombre de vers
- void setup() {
- size(1080,800);
- vers = loadStrings("vers.txt");
- font = loadFont("Ziggurat-HTF-Black-32.vlw");
- textFont(font,20);
- fill(0);
- smooth();
- plsphrases = new ArrayList();
- // verifier le nombre de vers
- int taillevers = vers.length;
- println(taillevers);
- //
- }
- void draw() {
- fill(0, 80);
- rect(0, 0, width, height);
- for (int i=plsphrases.size()-1; i>=0; i--) {
- Phrase phrase = (Phrase) plsphrases.get(i);
- phrase.place();
- phrase.bouge();
- phrase.fade();
- }
- }
- void mousePressed() {
- plsphrases.add(new Phrase(mouseX, mouseY)); }
- class Phrase {
- float x;
- float y;
- color couleur;
- float vitesseY;
- int choixvers;
- int opacite;
- int opmoins;
- Timer timer;
- Phrase (float nouvX, float nouvY) {
- x = nouvX;
- y = nouvY;
- couleur = 255;
- vitesseY = -1.5;
- choixvers = int(random(vers.length));
- opacite = 240;
- opmoins = int(random (1,5));
- // Timer
- timer = new Timer(1500);
- timer.start();
- }
- void place() {
- fill(couleur,opacite);
- textAlign(CENTER);
- text(vers[choixvers], x, y);
- }
- void bouge() {
- y = y + vitesseY;
- }
- void fade() {
- if (opacite>0 && timer.isFinished()){
- opacite = opacite - opmoins;
- }
- }
- }
- class Timer {
- int savedTime; // When Timer started
- int totalTime; // How long Timer should last
- Timer(int tempTotalTime) {
- totalTime = tempTotalTime;
- }
- // Starting the timer
- void start() {
- // When the timer starts it stores the current time in milliseconds.
- savedTime = millis();
- }
- // The function isFinished() returns true if 5,000 ms have passed.
- // The work of the timer is farmed out to this method.
- boolean isFinished() {
- // Check how much time has passed
- int passedTime = millis()- savedTime;
- if (passedTime > totalTime) {
- return true;
- } else {
- return false;
- }
- }
- }
1
