

Creating new methodsĪs well as using existing methods, programmers can create new methods.

The method ‘isKeyDown’ can also be used with letters such as “a”, and the other arrow keys. This also uses methods from the Greenfoot class.
#Getworld greenfoot code
The following code will allow an object to turn left or right when the left or right arrow keys are pressed. Moving an object using the keyboard controls Once it touches the edge of the World, it will turn away and continue its journey. This method will make the object move but turn randomly. If it touches the edge of the World it will turn 10 degrees. If that random number is less than 20, the code will generate another random number between -30 and 30 and turn that many degrees. The following code will move an object forward four spaces and then generate a random number between zero and 99 inclusive. Public static int getRandomNumber(int limit) The dot notation is necessary because getRandomNumber is a static method not associated with the class that it is defined in, so it can be accessed whenever necessary. This will access the getRandomNumber method of the Greenfoot class. To access these methods, use the dot notation, for example, Greenfoot.getRandomNumber(). The Greenfoot class provides methods to control the movement in a game. This method is created automatically, ready for code to be added to it. This is where any actions that the object will do happen, for example move(). Methods happen when a scenario is run.Įvery object that is an Actor class has a method called ‘act()’. For instance, the object could move forward, turn, make another object disappear, play a sound, etc. Mushroom m = (Mushroom) getOneIntersectingObject(Mushroom.In Greenfoot, a method is the name given to an action that has been programmed into an object. * Stops the scenario once this pig has eaten at least 15 mushrooms. * Follows the mouse movement and eats mushrooms on mouse clicks. * Constructs a Pig object and initializes it as having ** Keeps track of how many mushrooms this pig has eaten. GreenfootImage img = new GreenfootImage("barrel.png") įinal int originalWidth = img.getWidth() įinal int originalHeight = img.getHeight() Ĭlass Pig import greenfoot.* // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) * the initial state of this barrel - its (x,y) position and its * Automatically called by Greenfoot whenever a Barrel object * Returns how many mushrooms this barrel has stored. SetImage(new GreenfootImage("barrel.png")) * Returns this barrel to its original (x,y) location and its Int height = (int)(img.getHeight() * SCALE_FACTOR_25) Int width = (int)(img.getWidth() * SCALE_FACTOR_25) List nearby = getObjectsInRange(75, Mushroom.class) * If this barrel stores more than 10 mushrooms, this barrel has itself removed from

* Increases its current image scale by 25% when it eats five mushrooms. * Increases its current image scale by 5% when it eats one mushroom. MouseInfo mouse = Greenfoot.getMouseInfo() Private final double SCALE_FACTOR_25 = 1.25 Private final double SCALE_FACTOR_5 = 1.05 * Write a description of class Barrel here. My counter with shrooms never works out right.Ĭlass Barrel import greenfoot.* // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) However if I try and go into the Pig class and use Barrel.getMushrooms() it says non-static method getMushrooms() cannot be referenced from a static context.īut when I try using stuff like Barrel b1 = new Barrel() Remember, the mushrooms are already gone from the world - this happened when the barrel “stored them.” The pig can add this amount to its count of mushrooms eaten. The pig will have to call the barrel’s getMushroom() method to find out how many mushrooms the barrel currently stores. “the pig eats all the mushrooms currently in the barrel” So for my class I have to use the Greenfoot IDE.
