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

2014/2/10

calling a method from another class

duxbuz duxbuz

2014/2/10

#
I was trying to display an image using setImage(). I can do it one way leaf.setImage("skull.png"); but not via a method in leaf that I created. leaf.munched(); which basically calls setImage : this.setImage("skull.png"); If I create a new object: Leaf aLeaf = new Leaf(); I can access the munched() method I created. But if I try to access the method I created by using this : Actor leaf = getOneObjectAtOffset(0, 0, Leaf.class); The method is not available. Anyone tell me what I am getting confused about? Thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
     * Eat a leaf.
     */
Leaf aLeaf = new Leaf();
 
    public void eatLeaf()
    {
        Actor leaf = getOneObjectAtOffset(0, 0, Leaf.class);
        Actor wombat = getOneObjectAtOffset(0, 0, Wombat.class);
        if(leaf != null) {
            // eat the leaf...
        getWorld().removeObject(this);
        leaf.munched();
        //leaf.setImage("skull.png");
            leavesEaten = leavesEaten + 1;
        }
    }
danpost danpost

2014/2/10

#
Try 'this.munched();'. This is equivalent to:
1
2
Leaf leaf = this;
leaf.munched();
davmac davmac

2014/2/10

#
Your 'leaf' variable is of type 'Actor', and there is no 'munched' method in the Actor class. Your leaf variable needs to be of type Leaf. Change line 8 to:
1
Leaf leaf = (Leaf) getOneObjectAtOffset(0, 0, Leaf.class);
Also, line 4 looks like it's an accident - you should probably remove it.
danpost danpost

2014/2/10

#
danpost wrote...
Try 'this.munched();'. This is equivalent to:
1
2
Leaf leaf = this;
leaf.munched();
What davmac suggested! (I should have paid more attention to the title of this discussion)
duxbuz duxbuz

2014/2/10

#
Oh yes of course. I see this. So you look like you have cast it or something? Would it be the way to go or to declare new object like I did with my typo eaf leaf aLeaf = new Leaf();
duxbuz duxbuz

2014/2/10

#
Thanks by the way.
davmac davmac

2014/2/10

#
Would it be the way to go or to declare new object like I did with my typo
It is nearly always wrong to make a new object just to call one method on it. In this case, setting the image of a new object - an object which isn't even in the world - won't have any effect. You should call munched on the leaf you actually want to affect.
duxbuz duxbuz

2014/2/10

#
Now I noticed when I used your code and changed the actor type to type Leaf that calling my method seemed to work fine. But if I use my leaf aLeaf = new Leaf() when I call this method, I do not get expected outcome. I am unsure as to why this is. I thought my creating of a new object looked ok.
duxbuz duxbuz

2014/2/10

#
Ok thanks makes sense. The syntax of Leaf.class is the way to get to current object? That is at location getOneObjectAtOffset This way of accessing an object seems like nothing I have seen in my limited use of Java, is this a greenfoot idiosyncrasy or is this standard Java? I don't mean the getOneObjectAtOffset which is abviously part of greenfoots API..... I mean the Leaf.class part
davmac davmac

2014/2/10

#
Leaf.class is standard Java, though you don't see it much in general Java programming. Specifically it is a class literal. It is a way of accessing the object representing a particular class (in this case, Leaf!) so that you can, for instance, pass a class as a parameter to a method - which is what you're doing in this case.
You need to login to post a reply.