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

2012/4/27

I have problem with getHeight() method

snow13 snow13

2012/4/27

#
I have this code public void onground() { Actor under = getOneObjectAtOffset(0,getHeight()/2,ground.class); return under != null; } it says "cannot find symbol- method getHeight()",although this method exists in greenfoot library
davmac davmac

2012/4/27

#
Hint: what class does the getHeight() method exist in?
danpost danpost

2012/4/27

#
davmac's response was not intended to be a trick question. He meant, of the classes it does exist in, which one were you attempting to acquire the height in.
FallenPhoenix FallenPhoenix

2012/5/13

#
I believe that neither of those responses are very clear. i think that he did not want a subtle answer, but he would prefer a more straight up, out in the open answer. not many people like being answered with a riddle, and i am one person who would definitely like to know the answer to this one too.
darkmist255 darkmist255

2012/5/13

#
Well it's been a while since he's posted so either he figured it out or didn't come back. Think about what Davmac said: "what class does the getHeight() method exist in?" getHeight() is part of the GreenfootImage class, so we want to call the method from the class. You want the height of the image, so it's "getImage().getHeight()" Thus the code would've been
Actor under = getOneObjectAtOffset(0,getImage().getHeight()/2,ground.class);
danpost danpost

2012/5/13

#
If you look in the Greenfoot API, you will find that 'getHeight()' appears in not just one, but two, different classes. Only one of those two would be appropriate for what the code is trying to do. Figuring out the class, is not just a hint, but a tell-tale sign of what is needed to make the code compile (and it would not be hard to locate them, as the Greenfoot API is not that extensive). All non-static methods work on an object of some type. If the object is not explicitly given, than the compiler will check for a 'this' (which would be either the object that is being 'act()'ed on at the moment, or one that was explicitly given in the call to that method). Keeping what I just stated in mind, and noting that 'getHeight()' is a non-static method of a specific class of object, well..
darkmist255 darkmist255

2012/5/13

#
Oh yes the World class has a getHeight(), I forgot about that (even though I used it this morning :D, not the best memory)!
danpost danpost

2012/5/13

#
danpost wrote...
Figuring out the class, is not just a hint, but a tell-tale sign of what is needed to make the code compile
I probably should have wrote 'to make the code work as you want it to', as it would compile with 'getWorld().getHeight()' as well! @darkmist255, my post was not in response to yours; as it took a little time to put my thoughts in words and I had not seen your post until after I had posted my reply. :+)
davmac davmac

2012/5/13

#
i think that he did not want a subtle answer, but he would prefer a more straight up, out in the open answer
That may be true; certainly a lot of people come to these forums and essentially ask other people to fix their code for them. However, I prefer to make them think about how to fix it theirself. In this case I wanted snow13 to understand that when you call a method that's defined in another class, you need to somehow specify which class (and perhaps which object) has the method you want to call. If snow13 had still been confused, they could have responded and asked for more help.
FallenPhoenix FallenPhoenix

2012/5/13

#
davmac wrote...
i think that he did not want a subtle answer, but he would prefer a more straight up, out in the open answer
That may be true; certainly a lot of people come to these forums and essentially ask other people to fix their code for them. However, I prefer to make them think about how to fix it theirself. In this case I wanted snow13 to understand that when you call a method that's defined in another class, you need to somehow specify which class (and perhaps which object) has the method you want to call. If snow13 had still been confused, they could have responded and asked for more help.
I do agree with you as to letting them think about what they are doing, and i think that it would normally help. thanks to another, similar forum post, i found the solution. make a new method that says the following: public int getHeight() { return getImage().getHeight(); } that lets the program know that you want the height of the image, and that is the problem with the getHeight() method. when you put it in, i doesnt know what you want to get the height of.
programmer274 programmer274

2012/12/10

#
usually when I ask a question I try not to ask for the straight question and if I do I ask what each part of the code does.
You need to login to post a reply.