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

2018/1/28

Infinite loop

ImAndrew ImAndrew

2018/1/28

#
I tried to prepare a room with some loops but now my Greenfoot is freezing, saying I have an infinite loop and starting another program alone.I want to place the room in the middle of the screen because its size will be different from the screen size. Sometimes it creates me the room but I should wait and see how it opens me a lot of new windows with the game.The method is called from the world constructor.
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
void prepareRoom(int width, int height)
 {
     int _width = --width;
     int _height = --height;
      
     int centerX = WIDTH/2;
     int centerY = HEIGHT/2;
     int size = getCellSize();
     
     int x1 = centerX - width/2;
     int x2 = centerX + width/2;
     int y1 = centerY - height/2;
     int y2 = centerY + height/2;
      
     for(int i = y1; i < y2; i++)
     {
         for(int j = x1; j < x2; j++)
         {
             if(i < height/6)
             {
                 addObject(new Wall(size,size), j, i);
             }
             else if(j < 2 || j > _width - 2 || i > _height - 2)
             {
                 addObject(new Wall(size,size), j, i); //it will be wall2
             }
             else
             {
                 addObject(new Floor(size,size), j, i);
             }
         }
     }
 }
danpost danpost

2018/1/28

#
You may not have an infinite loop -- but, you will have a very large number of actors created when the size of a cell is small (or one). You have enough information to create just 5 actors (the 4 walls and the floor) without having to use any loops.
ImAndrew ImAndrew

2018/1/28

#
danpost wrote...
You may not have an infinite loop -- but, you will have a very large number of actors created when the size of a cell is small (or one). You have enough information to create just 5 actors (the 4 walls and the floor) without having to use any loops.
The size cell is 5 (first was 1).How could I create the walls and the floor without loops?
danpost danpost

2018/1/28

#
ImAndrew wrote...
How could I create the walls and the floor without loops?
Basically (if using a cell size of 1) this should work:
1
2
3
4
5
6
7
8
9
10
Floor floor = new Floor(width, height);
addObject(floor, centerX, centerY);
Wall wall = new Wall(width, height/6);
addObject(wall, centerX, height/12);
wall = new Wall(width, 3);
addObject(wall, centerX, height-1);
wall = new Wall(3, height);
addObject(wall, 0, centerY);
wall = new Wall(3, height);
addObject(wall, width-1, centerY);
danpost danpost

2018/1/28

#
The floor could actually be the background of the world and you then you would not need that particular actor (or its class). With a cell size of 5, you would need to perform a few checks and adjust image sizes and placements to properly place the walls.
ImAndrew ImAndrew

2018/1/28

#
danpost wrote...
ImAndrew wrote...
How could I create the walls and the floor without loops?
Basically (if using a cell size of 1) this should work:
1
2
3
4
5
6
7
8
9
10
Floor floor = new Floor(width, height);
addObject(floor, centerX, centerY);
Wall wall = new Wall(width, height/6);
addObject(wall, centerX, height/12);
wall = new Wall(width, 3);
addObject(wall, centerX, height-1);
wall = new Wall(3, height);
addObject(wall, 0, centerY);
wall = new Wall(3, height);
addObject(wall, width-1, centerY);
You used one Wall variable to create 4 wall objects?
danpost wrote...
The floor could actually be the background of the world and you then you would not need that particular actor (or its class). With a cell size of 5, you would need to perform a few checks and adjust image sizes and placements to properly place the walls.
Well, I want to have a level design like Pokemon's with the room in center of the screen and a dark background around.
danpost danpost

2018/1/28

#
ImAndrew wrote...
You used one Wall variable to create 4 wall objects?
I was done with one object before I went on to the next and the variable was already there. Why not?
I want to have a level design like Pokemon's with the room in center of the screen and a dark background around.
Should not be a problem.
ImAndrew ImAndrew

2018/1/28

#
danpost wrote...
ImAndrew wrote...
You used one Wall variable to create 4 wall objects?
I was done with one object before I went on to the next and the variable was already there. Why not?
I want to have a level design like Pokemon's with the room in center of the screen and a dark background around.
Should not be a problem.
I thought that would cause errors but I see it didn't. I have another problem related to creating worlds.How could I shrink and enlarge the object image without losing its quality. With the scale method wich I use the objects images look very bad and pixelated.Could I use vector images?
danpost danpost

2018/1/29

#
ImAndrew wrote...
I thought that would cause errors but I see it didn't.
I am not declaring a new variable with the same name. Just re-using an already existing variable.
How could I shrink and enlarge the object image without losing its quality. With the scale method wich I use the objects images look very bad and pixelated.Could I use vector images?
You should not, in this case, re-scale the same image over and over. Copy the original image and scale it each time::
1
2
3
GreenfootImage img = new GreenfootImage(image);
img.scale(??, ??);
setImage(img);
where image is the original image.
You need to login to post a reply.