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

2015/4/3

infinite loop problem

fejfo fejfo

2015/4/3

#
I have a program and if a player comes near a chest I want to give him extra lives and go to the next level. some times if you go to the next level I get an infinite loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void act()
    {
        if(getObjectsInRange(50,Player.class).isEmpty() == false) {
           System.out.println("active");
           Player p = (Player) getObjectsInRange(50,Player.class).get(0);
           System.out.println("got player");
           p.lives+=lives;
           System.out.println("gave it lives");
           getWorld().removeObject(this);
           System.out.println("removed me");
           Greenfoot.setWorld(new RandomisedLevel(level+1));
           System.out.println("set the new world");         
        }       
    }
if I do
1
Greenfoot.setWorld(new RandomisedLevel(level+1));
manually it doesn't stop. and it prints all of the print statements this is the constructor of RandomisedLevel
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
48
49
50
51
52
53
54
55
56
57
58
59
60
private int level;
    private double dif; //lower is harder
    static final int maxXdiference = 125;   
    static final int maxYdiference = 50;
    static final int minY = 100;
    static final int minX = 20;
     
    private int x;
    private int y;
    public RandomisedLevel(int level)    {
        super(600,400,1);
        this.level = level;
        dif = 1.0/level; //0.5
        int maxMobs = level+1; //2
         
        makePlatforms();
         
        for(int i = 0; i < maxMobs; i++) {
         
        }
        //addObject(new Player(),20,550);
    }
     
    private void makePlatforms() {
        int maxPlatforms = level+3;   //4    
         
        int platLenght = 50 - ((int) (level*dif*5)); //45
         
        int xDif =(int) platLenght+10+level*2; //59
        int yDif = (int) 20+level*2; //24
        if(xDif > maxXdiference) xDif = maxXdiference;       
        if(yDif > maxYdiference) yDif = maxYdiference;
         
        setX(minX);
        setY(Greenfoot.getRandomNumber(getHeight()));
        addObject(new Platform(platLenght+Greenfoot.getRandomNumber(20)-10),x,y);
        addObject(new Player(),x,y-25);
        for(int i = 0; i < maxPlatforms-1; i++) {
            setX(x+xDif+Greenfoot.getRandomNumber(20)-10);
            int stateToken = Greenfoot.getRandomNumber(2);
            if(stateToken == 0) stateToken = -1;
            setY(y+(yDif+Greenfoot.getRandomNumber(20)-10)*stateToken);
            addObject(new Platform(platLenght+Greenfoot.getRandomNumber(20)-10),x,y);
            if(x+10 >= getWidth()) {
                addObject(new Chest(level),x,y-25);
                return;
            }
        }
        addObject(new Chest(level),x,y-25);
    }
     
    private void setX(int x) {
        if(x < minX) x = minX;
        this.x = x;
    }
     
    private void setY(int y) {
        if(y < minY) y = minY;
        this.y = y;
    }
why does it stop ? why does it only stops sometimes ? how can I fixs this ?
fejfo fejfo

2015/4/4

#
help please ?
Super_Hippo Super_Hippo

2015/4/4

#
I don't see where this is causing an infinite loop. Did you try to wait a minute? In the constructor of the Platform class, do you create the image manually? This could take a while if you create multiple platforms then.
danpost danpost

2015/4/4

#
Are you getting an error message when it stops? if so, what is it? (copy/paste in entirety)
You need to login to post a reply.