The next way to improve the game is to animate the movements of the crab. Now you have a version of the crab project that will bring all the actors in to the world whenever the scenario is compiled. You will do that be replacing your coordinate values will calls to get some random numbers from the Greenfoot class. Test it.Ĥ.5 Do this: Use random numbers for where the worms are going to appear. Then you have to call it from the constructor. You have to declare the populateWorld yourself, it has no parameters and declares nothing. The last two parameters tell where to put the crab on the x, y plane.Ĥ.1 Do this: Add code to your CrabWorld constructor to make a crab automatically like you saw above.Ĥ.2 Do this: Add code to make 3 Krakens automatically too, you can choose a spot for each of them yourself.Ĥ.3 Do this: Add code to create 10 worms at places of your choosing in the world.Ĥ.4 Do this: Take all the code you just made that creates the objects into a separate method called populateWorld, in the CrabWorld class. The easy way to do that is use it instead of the actor parameter of the addObject to add this object to our world: Since we have created a new object, we have to do something with it. We didn’t put in any parameters in the example above, that’s why the parentheses () are empty. The expression to create new objects always has the keyword new at the start, then the name of the class we want to create, and finally a parameter list. It will make a new instance of class Crab. The Java keyword new lets you create new objects of any existing classes. THE PILLARS OF CREATIONĪddObject will method lets us add an actor object to the world, but we must have an object to add first. We can use this to add new actors to the world, and since the method is part of World class and CrabWorld is a subclass of World it inherits the ability to do this too. The type of the object parameter is Actor, and the x and y both have to be int (integers). The method requires 3 parameters, the object, x and y The method doesn’t return a result (since it’s void return type) Void addObject(Actor object, int x, int y) You can find it by looking up the class documentation for the World class. The addObject method is a method that belongs to the World class. The addObject method, and the new statement which is used to create a crab. That means that the origin point (0,0) is the top left part of the world. That means 150 cells from the left edge of the world and 100 cells down from the top of the world. This will put a new crab at the location x=150, y=100 in the world. Any code put in to the constructor, like to create an actor, will be executed alongside the world creation code. Because this constructor is executed every time a world is created, it can also create our actors automatically. In this example the constructor builds the world to the size we want (560 cells by 560 cells) and a resolution of 1 pixel per cell. It then does what it wants to do to set up this new instance in to a starting state. The name of the constructor is always the same as the name of the class.Ī constructor is a special type of method that is always automatically executed whenever a new instance of this class is executed. While a constructor looks similar to a method there are some differences:Ī constructor has no return type specified between the keyword “public” and the name This part is the constructor of the class. The next part is where we are very interested: After that is the class header, and a comment. You can see the usual import statement in the very first line. Take a look at the source code for CrabWorld. World instances are different from actors, we have to create actors ourselves, but Greenfoot automatically creates one instance of our world class and displays that instance on screen. The world object, that you see on the screen (that sand-colored square area) is an instance of the CrabWorld class. One thing that does happen every time the scenario is successfully compiled: the world itself is created. Greenfoot should do that automatically, and we will change the source code so it does. The first thing we want to fix is that the crabs, kraken, and worms have to be placed manually in the game. You are welcome to add stuff to it on your own after this though, and you will have a few suggestions on what you might do. There is always more you can do, but we won’t improve it anymore. We are going to finish our improvements to the crab game in this unit. It’s free and opensource! Choose the standalone one on Windows, if you are using Windows. If you need to download Greenfoot at home, you can go to the Greenfoot site:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |