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

2018/2/6

Counter makes game Laggy

Skleyrow Skleyrow

2018/2/6

#
Hey. I`m currently working on a small Bomberman game. I want to have a time (300 secs) that will count down to 0 and then resets the game. I made a subclass of an Actor called Text ( by the user danpost) . This looks like this :
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot, and MouseInfo)
 
public class Text extends Actor
{
    public Text()
    {
        this("");
    }
 
    public Text(String text)
    {
        setText(text);
    }
 
    public void setText(String text)
    {
        setImage(new GreenfootImage(text, 24, Color.BLACK, new Color(0, 0, 0, 0)));
    }
    
}
Then i created this in my World1 class ( The subclass of World) :
    public void act(){       
    timer--;
    Text timerText = new Text();
    addObject(timerText, 12, 0); 
    timerText.setText("Time left: " + (timer));
    Greenfoot.delay(3);
    removeObject(timerText);
      if (timer == 0){
     
        Greenfoot.stop();
         Greenfoot.setWorld(new World1());
      }

   }
} 
But as soon as i do thath the game starts to become very laggy. Can someone help me out ?
danpost danpost

2018/2/6

#
You should just leave one Text object in the world and change its text when needed -- not create, add, delay and remove a new one every act cycle. For more detailed help, show your World1 class codes.
Skleyrow Skleyrow

2018/2/6

#
import greenfoot.*; 
public class World1 extends MyWorld {
    private int timer = 100;
    private int time = 0;
    public World1() {
       super(15,14,64);
       prepare();
    }
    private void prepare() {
      time = 1280;
      // 1 min == 320;
      for(int x = 0; x < 15; x++){
        Unbreakable_block ub = new Unbreakable_block();
        addObject(ub, x, 1);
      }
      for(int x = 0; x < 15; x++){
        Unbreakable_block ub = new Unbreakable_block();
        addObject(ub, x, 13);
      }
      for(int y = 2; y < 13; y++){
        Unbreakable_block ub = new Unbreakable_block();
        addObject(ub, 0, y);
      }
      for(int y = 2; y < 13; y++){
        Unbreakable_block ub = new Unbreakable_block();
        addObject(ub, 14, y);
      }
      for(int x = 2; x <= 12; x +=2){
        Unbreakable_block ub = new Unbreakable_block();
        addObject(ub, x, 3);
      }
      for(int x = 2; x <= 12; x +=2){
        Unbreakable_block ub = new Unbreakable_block();
        addObject(ub, x, 5);
      }
      for(int x = 2; x <= 12; x +=2){
        Unbreakable_block ub = new Unbreakable_block();
        addObject(ub, x, 7);
      }
      for(int x = 2; x <= 12; x +=2){
        Unbreakable_block ub = new Unbreakable_block();
        addObject(ub, x, 9);
      }
      for(int x = 2; x <= 12; x +=2){
        Unbreakable_block ub = new Unbreakable_block();
        addObject(ub, x, 11);
      }
      Positiv pos = new Positiv();
      addObject(pos, 1, 2);
      Negativ neg = new Negativ();
      addObject(neg, 13, 12);
      for(int i = 0; i < 4; i++){
          Invis_block ib = new Invis_block();
          int x = 0;
          int y = 0;
          if(i == 0){
             x = 2;
             y = 2;
          }else if(i == 1){
             x = 1;
             y = 3;   
          }else if(i == 2){
             x = 13;
             y = 11;   
          }else if(i == 3){
             x = 12;
             y = 12;   
          }
          addObject(ib, x, y);
        } 
      int i = 0;  
      while(i < 75){
        Breakable_block bb = new Breakable_block();
        int x = (int) (Math.random()*13)+1;
        int y = (int) (Math.random()*12)+2;
        if(count(x, y, Unbreakable_block.class) == 0 && count(x, y, Breakable_block.class) == 0 && count(x, y, Invis_block.class) == 0 && count(x, y, Player.class) == 0) {
           addObject(bb, x, y);  
           i++;   
        }
      }
    }
    public void act(){       
    Text timerText = new Text();
    addObject(timerText, 12, 0); 
    timerText.setText("Time left: " + (timer));
    Greenfoot.delay(3);
    removeObject(timerText);
      if (timer == 0){
     
        Greenfoot.stop();
         Greenfoot.setWorld(new World1());
      }

   }
} 


                                 

    
    
Skleyrow Skleyrow

2018/2/7

#
danpost wrote...
show your World1 class
Done.
danpost danpost

2018/2/7

#
(1) Why did you remove the 'timer--;' line from the act method? (2) Is the purpose of the 'delay' to just show the text or is it also to control the game speed? (3) How many seconds (about) would you like the game to go?
Skleyrow Skleyrow

2018/2/7

#
danpost wrote...
(1) Why did you remove the 'timer--;' line from the act method? (2) Is the purpose of the 'delay' to just show the text or is it also to control the game speed? (3) How many seconds (about) would you like the game to go?
Sooo I found the problem but I don't know how to solve it. It's about the delay thing. I want the counter to display 300 sec and then count down to 0 no matter how high the game speed is. The Greenfoot.delay effects the whole world. That causes the lag. Do you know how I could create the timer?
danpost danpost

2018/2/7

#
Skleyrow wrote...
I want the counter to display 300 sec and then count down to 0 no matter how high the game speed is.
Are you saying that your game will run in more than one speed?
Skleyrow Skleyrow

2018/2/7

#
danpost wrote...
Skleyrow wrote...
I want the counter to display 300 sec and then count down to 0 no matter how high the game speed is.
Are you saying that your game will run in more than one speed?
Its a Bomber man Game so i want to have a power up which changes the game speed. This will include the Timer right ?
danpost danpost

2018/2/7

#
Skleyrow wrote...
Its a Bomber man Game so i want to have a power up which changes the game speed.
The power up should only make the bomber man actor move faster. You would not want the enemies to move faster as well. Then, what would be the point of the power up (in fact, it may make it a negative power up as quicker accurate reaction times would be needed by the user to avoid the enemies). What you want to do is regulate the speed of the bomberman with a timer to produce a delay between each step in its movement. Then lower the limit of the timer to allow the actor to move more often within the same amount of time. The enemies' movement should also be controlled by a timer; otherwise, they would move faster than the bomberman to begin with. After you get the timers in place, you can adjust the scenario speed as needed. I do not know what ratio you want between normal movement and powered up movement. Let us say you wanted the actor to move half again normal speed when powered up (a speed ratio of, normal to powered up, 2:3). Here, the timer limits would be 3 for normal speed and 2 for powered up (the inverse of the ratio when referring to time instead of speed). In 6 act cycles, only 2 steps are made when timer limit is 3 and 3 steps are made when timer limit is 2 (2*3 = 3*2 = 6). The timer is for the delay between steps. The higher its limit the longer the wait between steps -- or the slower the actor. Once you determine the scenario speed, you will need to test the game timer in the world class to see how many act cycles, on average, happen every second. Then you can come up with a fair limit for that timer (300 times amount of change in timer per second)..
You need to login to post a reply.