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

2019/3/12

having trouble understanding randomAsteroid()

lprasgla lprasgla

2019/3/12

#
I was going through the code of one of my classmates and there's this line I cannot understand: Asteroid.anz++; in public void randomAsteroid(){ Asteroid.anz++; if ( Greenfoot.getRandomNumber(100+(5*Asteroid.anz)) < 5 ) { Asteroid asteroid = new Asteroid(); int x = Greenfoot.getRandomNumber(getWorld().getWidth()); int y = Greenfoot.getRandomNumber(getWorld().getHeight()); getWorld().addObject(asteroid, x, y); } } anz is a public static int variable declared in the class Asteroid: public static int anz = 0; the method is also defined in the class Asteroid. I'm a rookie and my question is - why do I have to mention the class in Asteroid.anz++? When I delete "Asteroid" from the line the code works but I get multiple asteroids in the universe. And when it is there, there's only one asteroid. Why is it like this? :)
danpost danpost

2019/3/12

#
lprasgla wrote...
why do I have to mention the class in Asteroid.anz++? When I delete "Asteroid" from the line the code works but I get multiple asteroids in the universe. And when it is there, there's only one asteroid. Why is it like this? :)
You will need to show the code about which the method is called and indicate what class that code is in.
You need to login to post a reply.