

Again this is a parameter value, here the name of a sound file. The aySound method then plays the sound file ‘eating.wav’.That leaf is then removed from the World.
#GREENFOOT SOUND CODE#

‘Leaf.class’, in brackets, is a value, called a parameter value. getOneIntersectingObject is a method looking for an object from the Leaf class that the Wombat is intersecting with.A local variable ‘Leaf’ has been created for the Actor class in this method.It will have been called from the act() method. This is a method written by the programmer.There are several things happening in the code to initiate different actions, as follows: The Leaf will be removed and an ‘eating’ sound will be played.Īctor leaf = getOneIntersectingObject(Leaf.class) If they are, the Wombat needs to look as though it has eaten the Leaf. The code below will check to see if the Wombat and the Leaf are in the same position. Suppose the new method is called ‘ke圜ontrols()’.Ĭollisions are often used to initiate actions. The format of the heading of a new method is:Ī method is called from the act() method. These work like functions in other programming languages. 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. 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. In Greenfoot, a method is the name given to an action that has been programmed into an object.
