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

2015/1/15

help cannot find symbol

samwertwert samwertwert

2015/1/15

#
I want to get the age of the fish2 into the fish1, but fish2.getAge() is always not found. Yet the getAge() in fish2 does return the age of itself, only that fish1 cannot detect getAge(). fish1
 public void eat()
    {
        int width = getImage().getWidth();
        int height = getImage().getHeight();
        Actor fish2 = getOneIntersectingObject(fish2.class); 
        int i = 15;
        if  (fish2   !=  null)   
        {   
            int j = fish2.getAge();
            if (j< i)
            {
                World sea = getWorld();
                sea.removeObject (fish2);  
                GreenfootImage img = new GreenfootImage("fish2_left_medium.jpg");
                direction = 0;
                img.scale(width+1, height+1);
                setImage(img);
                if (i< 77)
                {
                    i = i+1;
                }
fish2
int age = 0;
    int r = Greenfoot.getRandomNumber(5)+1;
    public fish2(int x, int y)
    {
        GreenfootImage img = new GreenfootImage("fish3_left_medium.jpg");  
        img.scale(x, y);
        setImage(img);
        getAge();
        age = y;
    }

    /**
     * Act - do whatever the fish2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        int xCoord = getX();
        move(-r);
        getAge();
        if (xCoord <=0)
        {
            World sea = getWorld(); 
            sea.removeObject (this); 
        }
    } 
    
    public int getAge()

    {
        return age;
    }
davmac davmac

2015/1/15

#
First problem: class names should begin with a capital letter, i.e. the classes should be called Fish1 and Fish2, to comply with Java conventions. Now, line 5 in the fish1 code:
       Actor fish2 = getOneIntersectingObject(fish2.class);
You have declared fish2 to be an 'Actor', and the Actor class doesn't have the getAge() method, which is why you get an error. You need to declare fish2 to be of type Fish2:
       Fish2 fish2 = (Fish2) getOneIntersectingObject(fish2.class);
The cast is necessary as getOneIntersectingObject returns Actor.
samwertwert samwertwert

2015/1/15

#
Thank You very much, It is working now.
You need to login to post a reply.