Animating outside draw()
in
Programming Questions
•
3 years ago
Hello,
When I press mouse I want ellipse to animate for few seconds, then I want it to disappear from memory.
It shouldn't be inside main processing draw().
I tried a Thread, but ellipse has ugly flicker all the time:
- SimpleThread thread1;
- void setup()
- {
- size(400,300);
- }
- void draw()
- {
- background(0);
- float x=sin(frameCount*.01)*100+width/2;
- float y=cos(frameCount*.01)*100+height/2;
- ellipse(x,y,40,40);
- }
- void mousePressed()
- {
- thread1 = new SimpleThread(5,"cat");
- thread1.start(new PVector(mouseX, mouseY));
- }
- // Class taken from http://www.shiffman.net/teaching/a2z/threads/
- public class SimpleThread extends Thread{
- private boolean running; // Is the thread running? Yes or no?
- private int wait; // How many milliseconds should we wait in between executions?
- private String id; // Thread name
- private float count; // counter
- private PVector p;
- public SimpleThread (int w, String s) {
- wait = w;
- running = false;
- id = s;
- count = 0;
- p=new PVector(0,0);
- }
- // Overriding "start()"
- public void start (PVector p_)
- {
- p=p_;
- // Set running equal to true
- running = true;
- // Print messages
- System.out.println("Starting thread (will execute every " + wait + " milliseconds.)");
- // Do whatever start does in Thread, don't forget this!
- super.start();
- }
- // We must implement run, this gets triggered by start()
- public void run ()
- {
- while (running && count < 10) {
- //System.out.println(id + ": " + count);
- count+=.001;
- // Ok, let's wait for however long we should wait
- try {
- sleep((long)(wait));
- }
- catch (Exception e) {
- }
- float x=sin(frameCount*.01)*100+p.x;
- float y=cos(frameCount*.01)*100+p.y;
- ellipse(x,y,10,10);
- }
- System.out.println(id + " thread is done!"); // The thread is done when we get to the end of run()
- }
- // Our method that quits the thread
- public void quit()
- {
- System.out.println("Quitting.");
- running = false; // Setting running to false ends the loop in run()
- interrupt(); // in case the thread is waiting. . .
- }
- }
Any tips appreciated.
jimmi
1