Results 1 to 3 of 3
  1. #1
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    86
    Rep Power
    0

    Default Using Timer when adding a sprite to an array is not working

    Hello. I am trying to simulate clouds and rain on a JPanel. One of the problems I faced is to randomize the time when a rain drop falls so that they all do not fall at the same time. At the moment, I am trying to draw a rain drop, then wait for a second, then add another drop, wait for a second, add the third drop, etc. I am trying to use the Swing Timer as follows.

    Java Code:
     /**
         * Renders each rain drop.
         * @param g2d Graphics2D
         */
        private void nodeRain(Graphics g)
        {
            if(clouds.size()>0)
            {
                //  Get the lowest Y point of the clouds on the scene. This will be used to start the rain drop.
                int lowestY = 0;
                int lowestI = 0;
                for(int i=0;i<clouds.size();i++)
                {
                    if(clouds.get(i).getY()>lowestY)
                    {
                        lowestY = clouds.get(i).getY();
                        lowestI = i;
                    }
                }
                
                // Start drawing rain drops when the very first cloud reaches the edge of the scene.
                if(clouds.get(0).getX()<5)
                {
                    // Random location of the rain drop on the X axis
                    int cloudX = 0 + (int)(Math.random() * ((box_width - 0) + 1));
                        
                    // Location of the rain drop on the Y axis is aligned with the lowest cloud in the scene
                    int cloudY = lowestY+clouds.get(lowestI).getNewHeight()/2;
                        
                    // Add timer
                    ActionListener action = new ActionListener()
                    {   
                        @Override
                        public void actionPerformed(ActionEvent event)
                        {
                           
                            if(rains.size()<11)
                            {
                                addRain(cloudX,cloudY);
                            }
                        }
                    };
                       
                    timer = new Timer(1000, action); 
                    timer.setInitialDelay(0);
                    timer.setRepeats(true);
                    timer.start();
                }
            }
    
            // Render the rain drop sprite
            for(int i = 0; i <= rains.size() - 1; i++)
            {
                rains.get(i).render(g);
            }
        }
        
        
        /**
         * Adds rain drops to the array and removes them when their lives end.
         */
        public void updateRain()
        {
            // If the life of a particle ends (update is true), it must be removed from the array
            for(int i = 0; i <= rains.size() - 1;i++)
            {
                if(rains.get(i).update())
                {
                    rains.remove(i);
                } 
            }
        }
        
    
        /**
         * Creates a new rain drop sprite.
         * @param rainX Location of a rain drop sprite on the X axis
         * @param rainY Location of a rain drop sprite on the X axis
         */
        public void addRain(int rainX, int rainY)
        {
            // Speed of rain drops
            int dx = -1;
            int dy = 5;
            
            // Add a new particle to the array
            rains.add(new RainSprite(rainX,rainY, dx, dy, box_height));
        }
    It still adds all 10 drops at the same time without waiting. Any ideas what I am doing wrong?

    Thanks a lot!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    25

    Default Re: Using Timer when adding a sprite to an array is not working

    Can you put some debugging printlns in there to see what's going on?

    Just looking at the code I don't immediately spot the issue, but some debugging (with timestamps maybe?) might help to shed some light.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,170
    Rep Power
    12

    Default Re: Using Timer when adding a sprite to an array is not working

    And you may want to carefully examine your code to ensure that you are not adding them to the array (or list) somewhere else. Another thing I would do is disable the timer to see what happens. Or set it to fire only once.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Timer not working? help!
    By jackwong in forum AWT / Swing
    Replies: 5
    Last Post: 06-06-2012, 08:03 PM
  2. timer not working
    By PRW56 in forum New To Java
    Replies: 4
    Last Post: 03-28-2012, 04:54 PM
  3. Replies: 1
    Last Post: 03-28-2012, 12:41 PM
  4. Adding timer on my applet
    By jjackielz in forum Java Applets
    Replies: 4
    Last Post: 03-20-2011, 11:24 AM
  5. Confisuion about sprite.getX() and sprite.getY()
    By Basit781 in forum Java Gaming
    Replies: 0
    Last Post: 01-10-2011, 06:16 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •