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

2015/8/10

Cannot call a method from another class

marialo marialo

2015/8/10

#
Hi! I am having problems when I am trying to call a method from another class. What I basically want to do is to create a platform (ground) of two different colors that can be activated and thus become solid by touching another object (a cloud of the same color as the platform). Otherwise I want them to be transparent. It is shown in the video The Platform class extends Actor and the Cloud class extends Mover. So when I call my isSolid() method from Platform in Mover I get "cannot find symbol - method isSolid()" How can I fix it? Thanks in advance! The code in Mover:
/**
     * Check if the Mover is on the ground, ie is standing on a platform.
     * 
     * @return true if the Mover is on the ground, false if not.
     */
    public boolean isOnGround()
    {
        boolean onGround = false;
                  
        Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/2 , Platform.class);
       
        
        if(ground != null && ground.isSolid())       
        {
            moveOutOfGround(ground);
            onGround = true;              
        }        
        return onGround;
    }
isSolid() in Platform:
public boolean isSolid()
    {
        boolean solid = true;
        
        Girl girl = getOneIntersectingObject(Girl.class);
        
        if(girl != null)
        {
            getObject(this).setTransparency(200);
            solid = true;
            
        }
        return solid;
    }
danpost danpost

2015/8/10

#
Line 9 in the 'isSolid' method is troublesome. First, 'getObject' is not a method anywhere that I know of. Second, you cannot use 'setTransparency' on an Actor object; but, you can on a GreenfootImage object. As well as that line, the method itself will always return 'true' -- the 'solid' variable is initially set to 'true' and nowhere within the method is it ever (conditionally or not) set to false. There are several things you will need to do for this. You will need a way to distinguish between the different colored Platform and Cloud objects. This will mean a field and public getter methods in both classes to return the value of their fields. The field could be int, String or Color in type. The 'solid' field is not required as you can check the transparency of the image of the platform to determine whether it is to be considered solid or not when the girl contacts it.
marialo marialo

2015/8/13

#
Could you please explain more how I can make the color of the Platform and Cloud objects distinguishable? Maybe give an example because I didn't really get it...
danpost danpost

2015/8/14

#
Maybe you should review the java tutorial pages starting >here< on the concepts of object-oriented programming.
You need to login to post a reply.