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

2021/2/14

Why is one Scenario being constructed and another doesn't get constructed??

Har8mes Har8mes

2021/2/14

#
I have two games that i'm working on. But one Game suddenly didn't want to load and because other did load, I re-installed greenfoot (I've tried restarting the programm but it didn't load). PS: I have no syntax errors and I've compiled every actor dozens of times.
danpost danpost

2021/2/14

#
Har8mes wrote...
one Game suddenly didn't want to load PS: I have no syntax errors and I've compiled every actor dozens of times.
Show codes. Start with MyWorld class (or your initial World subclass).
Har8mes Har8mes

2021/2/14

#
I've just noticed that my code got resettet although i have compiled it and saved it
public class MyWorld extends World
{
    
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 800x800 cells with a cell size of 1x1 pixels.
        super(800, 800, 1); 
        prepare();
        
    }
    
    public void prepare()
    {
        int x = Greenfoot.getRandomNumber(600);
        int x1 = Greenfoot.getRandomNumber(600);      
        while (x == 0)
        {
            x = Greenfoot.getRandomNumber(600);
        }
        while (x <= 800)
        {
            x = Greenfoot.getRandomNumber(600);
        }
        while (x == x1)
        {
            x = Greenfoot.getRandomNumber(600);
            x1 = Greenfoot.getRandomNumber(600);
        }
        while (x1 == 0)
        {
            x1 = Greenfoot.getRandomNumber(600);
        }
        while (x1 <= 800)
        {
            x1 = Greenfoot.getRandomNumber(600);
        }
        ElonMusksCar eleonMusksCar = new ElonMusksCar();
        addObject(eleonMusksCar,400,754);
        alien aliens = new alien();
        addObject(aliens, x, 48);
        
    }         
}
danpost danpost

2021/2/14

#
Har8mes wrote...
I've just noticed that my code got resettet although i have compiled it and saved it << Code Omitted >>
Line 24 will always be true and execution will never branch out of the while loop. Same with line 37.
danpost danpost

2021/2/14

#
If you want two unequal random numbers between 0 and 599, then
int x = Greenfoot.getRandomNumber(600);
int x1 = Greenfoot.getRandomNumber(599);
if (x1 >= x) x1++;
If not zero, as well:
int x = 1+Greenfoot.getRandomNumber(599);
int x1 = 1+Greenfoot.getRandomNumber(598);
if (x1 >= x) x1++;
Har8mes Har8mes

2021/2/14

#
hey danpost, Thnx for your quick reply but I fixed my Problem. As other Discussion stated about this sort of topic, my problem was that i had an infinite loop in line 24 to 27:
while (x <= 800)
        {
            x = Greenfoot.getRandomNumber(600);
        }
Where the loop asked while under 800 generate a new number. But the Number could only be under 800 because the program can only generate up to the number 600 in the line 18:
  int x = Greenfoot.getRandomNumber(600);
so thnx for your help though and i will still change the code as you have written.
You need to login to post a reply.