And a little bit more:
Implement the createUfo() method:
If the list of UFOs is empty, create a ship in the center at the top of the screen.
Implement the checkBombs() method:
You need to check whether a bomb collided with the ship.
Space (part 16)
- 14
Locked
Comments (1)
- Popular
- New
- Old
You must be signed in to leave a comment
Justin Smith
27 October 2022, 11:37
Another one that seemed really easy to me but (based on average number of attempts) seems to be tricking people up. Only thing I can think of is that maybe people aren't being careful when iterating through a list and removing objects from the list based on conditions. You have to remember that if you use the most natural iteration process (i.e. for(Object object : objects) ) you are going to run into a problem where it removes an object and changes the index values of the remaining objects.
This is an issue that is addressed much much earlier in the Java course, but if you have forgotten, there are many ways to handle it. The official solution is actually one I have not seen before and it's kind of interesting. My preferred method is to just reverse the order of iteration. For example, using for(int i = list.size()-1; i >= 0; i--). And then accessing the list with .get. This way, when objects are removed from the list, it only re-indexes elements that have already been evaluated.
0