Loading...
Logo
Processing Forum
I am new to Processing. I have been building a simple game where one has to shoot some random moving birds. Although, it seems pretty simple, yet I have been stuck with the following problem: I need that once a bird has been shot, an image(of explosion/gunshot) should be displayed for at least 1 second. I tried using millis() to time the animation, but to no avail. Is there a way, such that two draw() functions be executed at the same time: one is the normal draw() and other of the object I want to display?  Any workaround would be highly beneficial!

Replies(5)

Thanks, amnon.owed, it avoids me to paste once more these links... ;)

jigsaw004 seems aware of the usage of millis() but somehow failed to use it properly, perhaps mixing up actions.
No need for two draw(), you use booleans / conditions / functions to separate the actions in draw().

Now, if we could see some code (including the failed attempt), we could write about it more concretely...
@PhiLho & amnon.owed: Thanks for your consideration. It is ironic that I figured out a way to accomplish the task, soon after publishing the post. However, I would like to show the code and discuss the problem anyway, since any optimization,whatsoever, interests me. Here's the code:

Copy code
  1. GunExplosion ge_1=null;//The class storing the image which i want to display
     int a=0;                        //To store the time at which an object of the class(here ge_1)                                         //was created
    void setup()   
    {  
       
      size(400, 300);  
      smooth();  
      noStroke(); 
      textFont(fonta,20);
    }

    void draw()                                       
    {  
     background(100);  
     if(ge_1!=null){ 
       if(millis()-ge_1.getA()<70) //display the image only when till the time a+70 i.e. 
                                            //the image should be there on the screen for 70 milliseconds
       ge_1.display();
     }
    }

    void mouseReleased(){
        a=millis();                                                    //store the time of mouse click
        ge_1=new GunExplosion(mouseX,mouseY,a);//the class GunExplosion gets instantiated differently 
                                                                          //each time the mouse is clicked.
    }
    public class GunExplosion{                              //class containing the image.
        float x,y;
        int a;
        PImage ge1;
        GunExplosion(float x, float y, int a){
            ge1=loadImage("http://www.clker.com/cliparts/8/3/7/a/1256081038614928840red-yellow-explosion.svg.med.png");
            this.x=x;
            this.y=y;
            this.a=a;
        }
        void display(){
               pushMatrix();
               image(ge1,x-15,y-15,30,30);//centering the image according to the cursor
               popMatrix();
        }
        int getA(){
            return a;
        }
    }                                                                                                                                                         
Optimizations: since you will create several explosions, it is better to create the image only once, in setup(). Even more since you access a Web site (might be a slow connection), but still true when loading it from the hard disk.
Otherwise, it looks quite good. You can set ge_1 to null once the time out is attained.