![]() ![]() When the whole line is executed, the inner part of the code-the creation of the GreenfootImage object-is executed first. This creates the image object from the named image file (crab2.png). Note that we do two things in this line: We call the setImage method, which expects an image as a parameter:Īnd in the place of the image to use, we write SetImage( new GreenfootImage( “crab2.png” )) We can create a GreenfootImage object from an image file as described above, and then use the actor’s setImage method to use it for the actor. If you did the exercise above, you will have seen that one method to set an actor’s image expectsĪ parameter of type GreenfootImage. What are they called, and what are their parameters? What do they return? There are two methods that allow us to change an actor’s image. Every individual actor can decide to change its image at any time.Įxercise 4.6 Check the documentation of the Actor class. That does not mean, however, that all objects of the same class must always keep the same image. We assign an image to the class when we create it, and every object created from that class will receive, upon creation, a copy of that same image. By default, actors get their image from their class. The position of the crab’s legs in these images is slightly different (Figure 4.2).įigure 4.2 Two slightly different images for the crabĪll Greenfoot actors have images. To make the movement of the crab look a little better, we plan to change the crab so that it moves its legs while it is walking.Īnimation is achieved with a simple trick: We have two different images of the crab (in our scenario, they are called crab.png and crab2.png), and we simply switch the crab’s image between these two versions fairly quickly. The next thing we shall discuss in relation to the crab scenario is animation of the crab image. (If you have trouble, look into the little-crab-5 scenario in the book projects-it includes this code and the remaining changes from this chapter.) 4.3 Animating images You should now have a version of your crab project that places the crab, lobsters, and worms into the world automatically, whenever you compile your scenario. You can do this by replacing your coordinate values with calls to get random numbers from the Greenfoot class. Try it out.Įxercise 4.5 Use random numbers for the coordinates of the worms. You need to declare the populateWorld method yourself (it takes no parameters and returns nothing) and call it from the constructor. Exercise 4.4 Move all the code that creates the objects into a separate method, called (We will discuss this statement in detail later-for now it is enough to know that this line will always appear at the top of our Greenfoot classes.)Įxercise 4.3 Add code to create 10 worms at arbitrary locations in the CrabWorld. In this class, we see the usual import statement in the first line. (If you do not have your own crab game at this stage, use little-crab-4 for this chapter.) ![]() Let us have a look at the CrabWorld’s source code (Code 4.1). World instances are treated in a special way in Greenfoot: While we have to create instances of our actors ourselves, the Greenfoot system always automatically creates one instance of our world class and displays that instance on screen. The world object, as we see it on screen (the sand-colored square area), is an instance of the CrabWorld class. There is one thing that happens automatically every time we successfully compile: The world itself is created. It would be better if that happened automatically. The first problem that should be addressed is the fact that we always have to place the actors (the crab, lobsters, and worms) manually into the world. However, a few more things need toīe done. We are now getting close to having a playable little game. First, however, we will discuss a number of improvements in detail. ![]() We will suggest some ideas at the end of this chapter. Of course, a game is never finished-you can always think of more improvements that you can add. “Finish” here means that this is where we stop discussing this project in this book. In this chapter, we will finish the crab game. World initialization, setting images, animating imagesĬoncepts: constructors, state, instance variables (fields), assignment, new (creating ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |