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

2016/9/26

Call an object's method

Aniveal Aniveal

2016/9/26

#
I'm getting a "cannot find symbol - method getBuildable()".
1
2
3
4
5
6
7
8
9
MouseInfo mouse=Greenfoot.getMouseInfo();
        if(mouse!=null)
        {
            x=mouse.getX();
            y=mouse.getY();
            if(getOneObjectAtOffset(mouse.getX(),mouse.getY(),Terrain.class).getBuildable())
            this.setLocation(mouse.getX(),mouse.getY());
             
        }
And in the class Terrain there is
1
2
3
4
public boolean getBuildable()
{
return buildable;
}
I am really confused because I thought that I could use Actor.method() to call it... If you don't understand what i want to do: I am working on a strategy game and there are Grass and Water subclasses of Terrain. I want that you can only build something on a Grass Object. Thanks for your help :)
Super_Hippo Super_Hippo

2016/9/26

#
'getBuildable' is not a default Actor method. It is a Terrain method. This should work:
1
if( ((Terrain)getOneObjectAtOffset(mouse.getX(),mouse.getY(),Terrain.class)).getBuildable())
danpost danpost

2016/9/26

#
Super_Hippo wrote...
'getBuildable' is not a default Actor method. It is a Terrain method. This should work:
1
if( ((Terrain)getOneObjectAtOffset(mouse.getX(),mouse.getY(),Terrain.class)).getBuildable())
....provided a Terrain object is always returned. But I doubt that will be the case since any mouse movement will cause the code to execute. Besides that, the location the use of those offsets look at are not what you may think. The coordinates being looked at with what you are showing now would be: ( this.getX()+mouse.getX(), this.getY()+mouse.getY() ) This certainly cannot be what you wanted. I think, maybe, you would want to use something like
1
2
3
4
5
if (!getWorld().getObjectsAt(mouse.getX(), mouse.getY(), Terrain.class).isEmpty())
{
    Terrain terrain = (Terrain)getWorld().getObjectsAt(mouse.getX(), mouse.getY(), Terrain.class).get(0);
    if (terrain.isBuildable()) setLocation(mouse.getX(), mouse.getY());
}
I guess you could use the 'getOneObjectAtOffset' method, if you subtracted the x and y of the actor:
1
2
Terrain terrain = (Terrain)getOneObjectAtOffset(mouse.getX()-getX(), mouse.getY()-getY(), Terrain.class);
if (terrain != null && terrain.isBuildable()) setLocation(mouse.getX(), mouse.getY());
Aniveal Aniveal

2016/9/27

#
Thanks for the replies! Adding the (Terrain) helped. @danpost: You are right, that's exactly what I wanted. I think I would have found that out after testing though ;)
You need to login to post a reply.