Skip to content Skip to sidebar Skip to footer

Greenfoot How Do U Make an Actor Move Continually Wothiout Pressing Keyus

Greenfoot methods

In Greenfoot , a method is the name given to an action that has been programmed into an object. For instance, the object could move forward, turn, make another object disappear, play a sound, etc. Methods happen when a scenario is run.

Every object that is an Actor class has a method called 'act()'. This is where any actions that the object will do happen, for example move(). This method is created automatically, ready for code to be added to it.

public void act() { // other action code is added here }

Moving an object randomly

The Greenfoot class provides methods to control the movement in a game. To access these methods, use the dot notation, for example, Greenfoot.getRandomNumber(). This will access the getRandomNumber method of the Greenfoot class. 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. The definition of getRandomNumber is:

public static int getRandomNumber(int limit)

The following code will move an object forward four spaces and then generate a random number between zero and 99 inclusive. If that random number is less than 20, the code will generate another random number between -30 and 30 and turn that many degrees. If it touches the edge of the World it will turn 10 degrees. This method will make the object move but turn randomly. Once it touches the edge of the World, it will turn away and continue its journey.

public void act() { move(4); if (Greenfoot.getRandomNumber(100) < 20) { turn(Greenfoot.getRandomNumber(61)-30); } if (atWorldEdge()) { turn(10); } }

Moving an object using the keyboard controls

The following code will allow an object to turn left or right when the left or right arrow keys are pressed. This also uses methods from the Greenfoot class.

public void act() { move(4); if (Greenfoot.isKeyDown("left")) { turn(-3); } if (Greenfoot.isKeyDown("right")) { turn(3); } }

The method 'isKeyDown' can also be used with letters such as "a", and the other arrow keys.

Creating new methods

As well as using existing methods, programmers can create new methods. These work like functions in other programming languages.

The format of the heading of a new method is:

public void Method_Name ()

A method is called from the act() method. Suppose the new method is called 'keyControls()'.

public void act() { keyControls() } public void keyControls () { // add code }

Handling collisions

Collisions are often used to initiate actions. The code below will check to see if the Wombat and the Leaf are in the same position. If they are, the Wombat needs to look as though it has eaten the Leaf. The Leaf will be removed and an 'eating' sound will be played.

public void eatLeaf() { Actor leaf = getOneIntersectingObject(Leaf.class); if(leaf != null) { getWorld().removeObject(leaf); Greenfoot.playSound("eating.wav"); leavesEaten = leavesEaten + 1; if(leavesEaten == 10) { setImage("Win.png"); Greenfoot.stop(); } } }

There are several things happening in the code to initiate different actions, as follows:

public void eatLeaf()

  • This is a method written by the programmer. It will have been called from the act() method.

Actor leaf = getOneIntersectingObject(Leaf.class);

  • A local variable 'Leaf' has been created for the Actor class in this method.
  • getOneIntersectingObject is a method looking for an object from the Leaf class that the Wombat is intersecting with. 'Leaf.class', in brackets, is a value, called a parameter value.
  • The parameter value is returned and is stored in the variable 'Leaf'.

if(leaf != null)

  • If the value returned is not null then the next statements will execute. If the value is null then the program will return to the act() method.

getWorld().removeObject(leaf);

  • This code sends the value of 'leaf', as a parameter value, to the getWorld.removeObject method. That leaf is then removed from the World.

Greenfoot.playSound("eating.wav");

  • The Greenfoot.playSound method then plays the sound file 'eating.wav'. Again this is a parameter value, here the name of a sound file. Sound files can be imported into Greenfoot.

leavesEaten = leavesEaten + 1;

  • A variable 'leavesEaten' is incremented by 1. This will have been defined in an earlier part of the code.

if(leavesEaten == 10)

  • The code in a nested if statement is now executed when 'leavesEaten' equals 10.

setImage("Win.png");

  • An image is displayed, using the parameter value file name 'Win.png'.

Greenfoot.stop();

  • This statement will stop the simulation at this point.

Invoking methods directly

Methods can also be invoked separately from running a scenario. This will apply to a single method running.

A screenshot of the Greenfoot environment showing how a method is being invoked. A method being invoked from a running scenario in Greenfoot

Right-clicking on an object in the World will display the methods inherited from the Actor class. Selecting 'boolean isAtEdge()' will display the result directly:

A screenshot of the Greenfoot environment showing methods inherited from the Actor class. Viewing the methods inherited from the Actor class in Greenfoot

dicarlosibiler.blogspot.com

Source: https://www.bbc.co.uk/bitesize/guides/zfgr97h/revision/7

Postar um comentário for "Greenfoot How Do U Make an Actor Move Continually Wothiout Pressing Keyus"