This site requires JavaScript, please enable it in your browser!
Greenfoot back
TheSlorrow44
TheSlorrow44 wrote ...

2019/2/15

Swing Timer

TheSlorrow44 TheSlorrow44

2019/2/15

#
Is it a good idea to use Swing Timer? Does it affect the performance if I use it repeatedly?I use a swing timer like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public void act()
    {
        switch(act)
        {
            
            case 1:if(prepare)
                     {    //some code here
                          prepare=false;
                          timer_act.setInitialDelay(250);
                          timer_act.start();
                      }
                      else timer_act.start();
                      break;
            //some code here
         }
        //some code here
}
 
 ActionListener wait= new ActionListener()
    { public void actionPerformed(ActionEvent evt)
      {
          act*=-1;
          prepare=true;
      }
    };
    Timer wait_ = new Timer(1000,wait);
     
    ActionListener wave1= new ActionListener()
    { public void actionPerformed(ActionEvent evt)
      {
          wave();
      }
    };
Timer timer_act = new Timer(1000,wave1);
 
 private void wave()
    {
        switch(act)
        {
          case 1://some code here     
                    enemy_number++;
                   if(enemy_number>15)
 {wait_.setInitialDelay(3000);wait_.start();act=-2;enemy_number=0;timer_act.stop();}
                   break;
         //some code here
       }
}
Should I stick with the common way ? :
1
2
3
4
5
timer = (timer +1)%100;
if(timer==0)
  {
     //some code here;
   }
danpost danpost

2019/2/15

#
TheSlorrow44 wrote...
Is it a good idea to use Swing Timer? Does it affect the performance if I use it repeatedly?I use a swing timer like this: << Code Omitted
For anything to be uploaded onto the greenfoot site, all javax.swing classes should be avoided (same with java.awt classes). The "common way", as you put it, is more direct, works anywhere and has no chance of messing with the behavior of the greenfoot environment.
You need to login to post a reply.