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

2012/3/8

Add new asteroids when all have been cleared

1
2
sophiasturges sophiasturges

2012/3/8

#
I am trying to create levels of the asteroid game in the greenfoot book and am stuck. When the scenario initializes, two asteroids are created in the space class. Here is the code for that: public class Space extends World { private int startAsteroids = 2; addAsteroids(startAsteroids); } The addAsteroids() method code is this: /** * Add a given number of asteroids to our world. Asteroids are only added into * the left half of the world. */ private void addAsteroids(int count) { for (int i = 0; i < count; i++) { int x = Greenfoot.getRandomNumber(getWidth()/2); int y = Greenfoot.getRandomNumber(getHeight()/2); addObject(new Asteroid(), x, y); } } Then there is a method in the Asteroid class which splits the asteroid into smaller pieces, or removes it from the world when it reaches a certain size called breakUp(). The breakUp() method is called from the hit() method. here is that code: /** * Hit this asteroid dealing the given amount of damage. */ public void hit(int damage) { stability = stability - damage; if(stability <= 0){ breakUp ();} } I was thinking of adding a new method called levelUp() after this breakUp() line of code. There is a method called numberOfObjects() in the world class which returns the number of actors in the world, but there are no examples of how to use it in the book, so I wasn't sure how to implement it. Here is what I have so far: /** * Adds one more asteroid each time a level is completed. */ private void levelUp() { int asteroids = numberOfObjects(); if (asteroids == 0) { Space space = (Space) getWorld(); space.addAsteroids(); } } public int numberOfObjects(); { return asteroids; } I was just trying to get the game to add asteroids to begin with by calling the addAsteroids() method from the space class and then I was going to try to make a loop counter to add one more to that each time. I got an error message saying "method addAsteroids in class Space cannot be applied to given types; required: int found: no arguments reason: actual and formal argument lists differ in length. Any ideas? Thanks for any help!
danpost danpost

2012/3/8

#
I believe the error message is trying to tell you that space.addAsteroids(); in the levelUp() method needs a parameter of integer type (maybe ++startAsteroids). The code provided is choppy, at best; and without the full error message and complete class code, it is difficult to say what is going on. Is that your whole world Space class code (I do not think how it can be)? (if not, please provide).
mjrb4 mjrb4

2012/3/8

#
The error is saying you need to pass an integer to the "addAsteroids()" method. In this code:
1
2
private int startAsteroids = 2;
addAsteroids(startAsteroids);
Assuming the latter statement is inside a block of code and not at the top level of the class you're calling the method correctly. However, there's another instance when you call that method here:
1
2
3
4
5
6
7
8
9
private void levelUp()
   {
       int asteroids = numberOfObjects();
       if (asteroids == 0)
       {
           Space space = (Space) getWorld();
           space.addAsteroids();
       }
   }
In that example, you're calling the addAsteroids() method with no parameters - that's what the error is complaining about. It takes an integer as an argument, and you haven't provided it with one. Something like this:
1
2
3
4
5
6
7
8
9
private void levelUp()
   {
       int asteroids = numberOfObjects();
       if (asteroids == 0)
       {
           Space space = (Space) getWorld();
           space.addAsteroids(1);
       }
   }
...should compile fine, though bear in mind I've just added 1 in there as an arbitrary value, I don't know if that's what you need!
sophiasturges sophiasturges

2012/3/9

#
@danpost. You were right. I did not include the whole space class because I did not want to overwhelm you with code. :) I am going to work on this tomorrow. I've literally worked on this off and on since last night and my head is spinning. Thanks again! Sophia
sophiasturges sophiasturges

2012/3/9

#
@mjrb4: You're right. That worked. I'm not sure how the compiler knows which objects to get in the numberofObjects method because it did not find that. I ended up trying to cast the addAsteroids method from the space class but it makes the game run buggy. I'm confused because I know how to initialize the scenario to create a set number of asteroids but not sure how to make more appear each time they are broken up or removed from the world. I can't find how to implement numberOfObjects anywhere. All I see are addObjects or removeObjects, not how to check whether or not objects of a certain class are 0. Anyways, I will do more research and try to ask more specific questions tomorrow. Thanks! Sophia
danpost danpost

2012/3/9

#
In the world class, you can just use
1
if (getObjects(Asteroid.class).isEmpty())
sophiasturges sophiasturges

2012/3/10

#
@danpost, That is a great idea! I suffered through it, lol and ended up using the list method to return all asteroids and then made the parameter if the asteroid is less than or equal to size 1, to level up and then remove the object. It works! I put the levelUp method in the space class and called it from the asteroid class. But I am so thankful for your willingness to help me. Thank you thank you. :)
pianoman598 pianoman598

2015/9/18

#
Hi guys, I know this discussion is 3 years old but I am trying to add asteroids to the scenario as well. And every time I add this line of code below, it says "cannot find symbol - method getWorld()". If you guys could help me out that would be awesome! Thanks :) private void levelUp() { int asteroids = numberOfObjects(); if (asteroids == 0) { Space space = (Space) getWorld(); space.addAsteroids(); } }
danpost danpost

2015/9/18

#
There was another problem with the code, as given previously in this discussion. The method 'numberOfObjects' is a World class method and the method 'getWorld' is an Actor class method. This means that the first would be executed on a World object and the other on an Actor object. Since neither specifies a specific object, both are trying to use the same object and that object cannot be both an Actor object AND a World object. Since you are getting complaints (an error message) on the 'getWorld' method, I would presume you placed the code in a World subclass. The code, therefore, can be re-written as follows:
1
2
3
4
private void levelUp()
{
    if (numberOfObjects() == 0) addAsteroids();
}
Actually, line 3, here, can be placed in the 'act' method of this World subclass and the 'levelUp' method can be removed.
pianoman598 pianoman598

2015/9/21

#
Hello danpost, I tried to paste line 3 of your code sample into the act method of the World subclass, and unfortunately it said, "method addAsteroids in class Space cannot be applied to given types; required: int; found: no arguments; reason: actual and formal argument lists differ in length". What am I doing wrong?
danpost danpost

2015/9/21

#
pianoman598 wrote...
Hello danpost, I tried to paste line 3 of your code sample into the act method of the World subclass, and unfortunately it said, "method addAsteroids in class Space cannot be applied to given types; required: int; found: no arguments; reason: actual and formal argument lists differ in length". What am I doing wrong?
Just put an int value in the round brackets after the method name to specify how many asteroids to add.
fairlykylie fairlykylie

2016/1/21

#
I'm using the last the last if statement you had but I changed it up, " if (getObjects(Asteroid.class).isEmpty()) { addAsteroids(startAsteroids); } " to fit my other methods, but instead of adding new asteroids, its not adding anything. I don't know what to do.
fairlykylie fairlykylie

2016/1/21

#
danpost wrote...
There was another problem with the code, as given previously in this discussion. The method 'numberOfObjects' is a World class method and the method 'getWorld' is an Actor class method. This means that the first would be executed on a World object and the other on an Actor object. Since neither specifies a specific object, both are trying to use the same object and that object cannot be both an Actor object AND a World object. Since you are getting complaints (an error message) on the 'getWorld' method, I would presume you placed the code in a World subclass. The code, therefore, can be re-written as follows:
1
2
3
4
private void levelUp()
{
    if (numberOfObjects() == 0) addAsteroids();
}
Actually, line 3, here, can be placed in the 'act' method of this World subclass and the 'levelUp' method can be removed.
danpost danpost

2016/1/21

#
fairlykylie wrote...
I'm using the last the last if statement you had but I changed it up, " if (getObjects(Asteroid.class).isEmpty()) { addAsteroids(startAsteroids); } " to fit my other methods, but instead of adding new asteroids, its not adding anything. I don't know what to do.
Where did you place this code snippet (which method -- what class -- under what conditions)? and how is 'startAsteroids' assigned a value? Also, what code do you have for the 'addAsteroids' method?
fairlykylie fairlykylie

2016/1/22

#
danpost wrote...
fairlykylie wrote...
I'm using the last the last if statement you had but I changed it up, " if (getObjects(Asteroid.class).isEmpty()) { addAsteroids(startAsteroids); } " to fit my other methods, but instead of adding new asteroids, its not adding anything. I don't know what to do.
Where did you place this code snippet (which method -- what class -- under what conditions)? and how is 'startAsteroids' assigned a value? Also, what code do you have for the 'addAsteroids' method?
Here's my class: public class Space extends World { private Counter scoreCounter;// the person who lives in the house private int startAsteroids = 3; private int startSpaceJunk = 2; public Space() { super(600, 400, 1); GreenfootImage background = getBackground(); background.setColor(Color.BLACK); background.fill(); createStars(500); Rocket rocket = new Rocket(); addObject(rocket, getWidth()/2 + 100, getHeight()/2); addAsteroids(startAsteroids); scoreCounter = new Counter("Score: "); addObject(scoreCounter, 60, 380); Explosion.initializeImages(); ProtonWave.initializeImages(); if (getObjects(Asteroid.class).isEmpty()) { addAsteroids(startAsteroids); } } /** * Create stars om the background * @param number the number of stars being created */ public void createStars(int number) { GreenfootImage background = getBackground(); for(int i=0; i < number; i++) { int x = Greenfoot.getRandomNumber( getWidth() ); int y = Greenfoot.getRandomNumber( getHeight() ); int color = Greenfoot.getRandomNumber(255); background.setColor(new Color(color,color,color)); background.fillOval(x, y, 2, 2); } } /** * Add a given number of asteroids to our world. Asteroids are only added into * the left half of the world. */ private void addAsteroids(int count) { for(int i = 0; i < count; i++) { int x = Greenfoot.getRandomNumber(getWidth()/2); int y = Greenfoot.getRandomNumber(getHeight()/2); addObject(new Asteroid(), x, y); } } /** * Add a given number of asteroids to our world. Asteroids are only added into * the left half of the world. */ /** * add score to the scorecounter * @param score - the value being added by the scoreCounter */ public void countScore(int score) { // add score using scoreCounter scoreCounter.add(1); } /** * This method is called when the game is over to display the final score. */ public void gameOver() { addObject(new ScoreBoard(scoreCounter.getValue()), 300, 200); } } it's a world class, hopefully its the right information.
There are more replies on the next page.
1
2