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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |