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
danpost danpost

2016/1/22

#
Okay -- there is no problem with the snippet, in itself. The problem is where you placed the snippet in your code. It is being executed one time -- when your world is being created. It is not being called while the world is running because it is placed in the constructor of the class. You need to place that code in the act method of the class for it to be continuously run (so that when the number of asteroids in the world becomes zero, more will be added). The World subclass template does not automatically create an act method like the Actor subclass template does; but, you there is an act method that you can override included in the World class too -- just add (override) the method yourself (add the method in your Start class). Then move the snippet into that method.
fairlykylie fairlykylie

2016/1/22

#
danpost wrote...
Okay -- there is no problem with the snippet, in itself. The problem is where you placed the snippet in your code. It is being executed one time -- when your world is being created. It is not being called while the world is running because it is placed in the constructor of the class. You need to place that code in the act method of the class for it to be continuously run (so that when the number of asteroids in the world becomes zero, more will be added). The World subclass template does not automatically create an act method like the Actor subclass template does; but, you there is an act method that you can override included in the World class too -- just add (override) the method yourself (add the method in your Start class). Then move the snippet into that method.
THANKS SO MUCH!!!!
Jonade21 Jonade21

2016/5/5

#
hello. can someone please help me as when I try to say Space space = (Space) getWorld(); it says that getWorld could not be found. btw this is in my space class. should it be somewhere else?
danpost danpost

2016/5/5

#
Jonade21 wrote...
hello. can someone please help me as when I try to say Space space = (Space) getWorld(); it says that getWorld could not be found. btw this is in my space class. should it be somewhere else?
The World class API does not contain a 'getWorld' method as does the Actor class API. However, if you are trying to code this in the Space class, you only need to use the 'this' keyword. That is:
1
Space space = this;
The thing about it is that you do not need this line at all in this class:
1
2
Space space = this;
GreenfootImage background = space.getBackground();
is the same as the following lines:
1
2
3
GreenfootImage background = this.getBackground();
// or this
GreenfootImage background = getBackground();
The method called could just as well be coded in the Space class (as opposed to the World class, where 'getBackground' is) in all cases.
You need to login to post a reply.
1
2