This site requires JavaScript, please enable it in your browser!
Greenfoot back
pr2alede
pr2alede wrote ...

2013/11/14

Coding Translation

pr2alede pr2alede

2013/11/14

#
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * This class defines a crab. Crabs live on the beach.
 * @Author Aled Evans
 * @version 1.0
 */
public class Crab extends Animal
{
    private GreenfootImage image1 = new GreenfootImage("crab.png");
    private GreenfootImage image2 = new GreenfootImage ("crab2.png");
    private Score score; ***
    private int counterx = 0;
    
    public Crab (Score pointcounter) ***
    {
         score = pointcounter; ***
    }
    
    
    public void act()
    {
      movement();
    counterx = counterx + 1;
    if(counterx==5){
        switchImage();
        counterx = 0;
}
lookForWorms();
}

public void lookForWorms()
{
    if(canSee(Worm.class)){
        eat(Worm.class);
        Greenfoot.playSound("slurp.wav");
        score.add(1);
    }
    if(score.getValue() == 5){
            Greenfoot.playSound("fanfare.wav");
            Greenfoot.stop();
        }
}

public void movement()
{
     move();
       if( atWorldEdge()){
           turn(5);
        }
if(Greenfoot.isKeyDown("left")){
turn (-12);
}
if(Greenfoot.isKeyDown("right")){
    turn (12);
    }
}

public void switchImage()
{
    if(getImage()== image1){
        setImage(image2);
    }else {
    setImage(image1);
}
}
}
I've added a score element to my game succesfully but not quite sure what the coding does excatly. I've basically taken a score class from another game and then added this score class to my controlled character (a Crab). But I don't quite understand how Score class and crab communicate with each other. I'll put the code below and highlighted the lines I don't quite understand what's happening by putting *** after them. Thankyou
Gevater_Tod4711 Gevater_Tod4711

2013/11/14

#
In line 12 you declare a new variable 'score' which type is the class Score. This variable is the reference to the score counter which (metaphorically) means that it is something like an arrow showing on the object in your RAM. Line 15 is the constructor of your Crab class. If you want to create a new crab you need to call this special method which constructs a new crab. The parameter scorecounter is again a reference to the Score object in your RAM. Because the reference scorecounter only exists in your constructor and not in the rest of your code you set the reference 'score' to the reference of 'scorecounter' because score is a global variable which is visible from everywhere in your crab class. If your crab now eats a worm you tell the score object to increase the score by one. Therefore you take the reference to the Score object and execute the method add(1) on this object. Because the world referes to the same object in the RAM like your crab class the image of your score object (which you see in the world) changes.
danpost danpost

2013/11/14

#
Another way to look at it: Line 12 creates an instance field for each Crab object created that can hold a Score object. Each Crab object created will have a similar field that can hold the same or different values. Line 15 declares a constructor method for the Crab class. This method is called when 'new Crab' is executed with arguments that fit the signature of the method. That is, the number of arguments and the order of the types of each argument must match. If 'new Crab(new Counter())' is execute, then the Crab constructor called must have the signature of 'Crab(Counter)'. Within the constructor, the Counter object is locally referred to by the field name following its type in the constructor declaration statement. That is, the local field name 'counter' in 'public Crab(Counter counter)' is what you use to refer to the Counter object within the constructor. Line 17 takes the Score object, referred to by the local field name 'pointcounter', and sets the instance Score field of the newly created Crab object to that object. Once the instance field is set, this Crab object will have access to that Score object throughout its existence. Line 37 accesses that Score object and calls a method on it to add an amount to its instance 'value' field. In short: Line 12: declares an instance field to hold a Score object Line 15: receives the Score object when the crab is created (passed from the calling method) Line 17: saves the Score object received in line 15 in the instance field declared at line 12 Line 37: executes the 'add' method on the Score object held by the instance field
pr2alede pr2alede

2013/11/14

#
Thankyou guys. Lots of help
You need to login to post a reply.