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

2011/12/13

Greenfoot Chapter 7 Problems

drumstick93 drumstick93

2011/12/13

#
I'm trying to make a new level for the asteroid program. I'm using the getObjectsInRange method to attempt to find when there are no more asteroids left, so i can initiate my custom made NewLevel() method in the space class...but I keep getting errors, because of the error: unmatched variable type. private void NewLevel() { List<Asteroid> asteroids = getObjectsInRange(225, Asteroid.class); for (Asteroid asteroid : asteroids) { if (asteroid = asteroids ) { Greenfoot.stop(); } } }
nccb nccb

2011/12/13

#
The main problem I can see is that your if condition is wrong. Firstly, you have a single equals in your if statement, which is an assignment. In Java, you need two equals signs, ==, to check for equality. Secondly, you are comparing asteroid (a single Asteroid item) to asteroids (which is a list of Asteroid items). Java won't let you compile this comparison because it's guaranteed to be false. And finally, if you want to find all asteroids in the world, use the World.getObjects method, which finds all objects regardless of range. So, if you just want to stop when there are no asteroids, try this:
List asteroids = getWorld().getObjects(Asteroid.class); 
if (asteroids.isEmpty())
{
    Greenfoot.stop();
}
drumstick93 drumstick93

2011/12/14

#
that actually helped alot, and worked really well...but it's an end of the year project I'm doing and I thought I should take it a step further. So instead of basing a change of level off the asteroids, I thought I'd base it off of the score, so i can have multiple levels. I know I need to assign a variable to the counter so i don't get the non-static method error, but I'm not sure how to do so. private void NewLevel() { Counter counter = Counter.getValue(); if (counter == 69) { Greenfoot.stop(); space.NewLevel(); setLocation(250,250); } }
You need to login to post a reply.