Implement fireworks thread inside oncreate method

134 Views Asked by At

I am trying to accomplish fireworks functionality based on the github code from Lenoids. I want to create two fireworks (one white and another red) and have them display continuously after every two seconds.

Here's what I did so far:

  1. Created two buttons (one for each color of fireworks). The buttons have the fireworks functionality.
  2. Call the buttons programatically in a thread to display the fireworks.

         @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_congratulations);
    
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    try {
                        Thread.sleep(100);
                        button10.callOnClick();  //for red fireworks 
                        button11.callOnClick();  //for white fireworks 
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
    
    
                }
            }
        }).start();
    }
    

The problem is that if I increase the value of sleep method, then the fireworks do not show up at all. How can I accomplish the two second continuous fireworks functionality?

Here's the fireworks code:

new ParticleSystem(MyClass.this, 100, R.drawable.star_pink, 800) .setSpeedRange(0.1f, 0.25f) .oneShot(view, 70);
1

There are 1 best solutions below

0
On

If you want to run the firework every two seconds then use Handler instead of the thread and thread.sleep.

final Handler ha=new Handler();
ha.postDelayed(new Runnable() {

@Override
public void run() {
   button10.callOnClick();   
   button11.callOnClick();
    ha.postDelayed(this, 2000);
 }
}, 2000);

Hope that helps.