

If you look at the bottom of the window above, you'll see that Greenfoot has provided us a useful error message and has even highlighted the code that has the problem. What do you think will happen? Click the "Compile" button to find out. Just for fun, let's compile this and see what happens. Wouldn't it be nice if there was a "followMouse()" function we could use? Let's pretend there is! Inside the act() method, type the following code: " followMouse() ". What we would like is to have the Avatar follow the mouse. Because there is no code in it, our Avatar will not move or display any other behavior when we run our scenario.

You can see the act() method we discussed above. You will see a screen pop up that like this:

Double-click on the Avatar to pull up the code editor (Note: you can also right-click on the class and select "open editor"). Let's add some code to the Avatar class that will allow us to control its movement using the mouse. If you are serious about learning Java and writing good Greenfoot scenarios, you should read that material. Oracle provides an excellent overview of Object-Oriented Programming Concepts. You have already done the "subclassing" and now it is time to add the code.Ĭlick this link to learn more about the World and Actor classes. You do this by subclassing the provided World and Actor classes to create new classes and writing code in them. (You right-clicked on those implementations above.) As a game programmer, you must code specific behaviors for your World and Actors. Greenfoot provides for you the code that implements general World and Actor behavior. It is the responsibility of each Actor to implement their act() method to describe their actions. The main responsibilities of the World is to add, remove Actors from the screen, and to periodically call the act() method of each Actor. Greenfoot views a program as a World that contains Actors. You should now have a scenario that looks like this: Again, hit the "Compile" button when done. Now, to create an enemy you do the same steps you just did for the hero, except choose "symbols->Smiley2.png" as the image and "Enemy" as the class name. In the main scenario window, hit the "Compile" button. In the "New class" popup window, name the the class "Avatar" and select the "symbols->skull.png" as the new class image. Start by right-clicking the Actor class in the scenario window (see picture below) and choosing "New subclass.". We will basically just follow the same steps we used to create a world class in the section above.

Let's create the character our players will control when they play our game. Those instructions will be abbreviated and you should refer back to this section for detailed instructions. In Part II of this tutorial, I require you to create two additional world classes. In the "New class" popup window, name the the class "AvoiderWorld" and select the "backgrounds->space1.jpg" as the new class image. We do this by right-clicking the World class in the scenario window (see picture below) and choosing "New subclass.". Next, we need to create a world for our game. Name the scenario "AvoiderGame" and then hit the "Create" button. Start Greenfoot and create a new scenario by clicking "Scenario"->"New.". Yeats Part I: Basic Game Elements Avoider Scenario “Education is not the filling of a pail, but the lighting of a fire.” “Do you know the difference between education and experience? Education is when you read the fine print experience is what you get when you don't.
