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

2011/6/17

Names of the Method

Busch2207 Busch2207

2011/6/17

#
For my next Project I use this Scrollworld: http://www.greenfootgallery.org/scenarios/3040 now I've got an Object that should move there. For that I wanted to use this Method:
public void move(double WalkingSpeed)
    {
     [...]
    }
but this Method only work, if I give her an other name... for example:
public void ChangePlace(double WalkingSpeed)
    {
     [...]
    }
and the rest of the Method is exactly the same... What's the reason for that? Are there some names of Methods you're not allowed to use in Greenfoot?
Builderboy2005 Builderboy2005

2011/6/17

#
What is the error that Greenfoot gives you? That might give some clue as to why you can't use that method
Busch2207 Busch2207

2011/6/17

#
If I scroll the world, the Object moves to the left uppon corner... And that's just, if the Method is called "move"...
mik mik

2011/6/17

#
As of Greenfoot 2.1, Actors have a move(int) method. So your actor inherits this from the Actor class. This leads to your actor having two move methods: move (int) -- inherited from Actor move (double) -- in your own class So far, there's nothing wrong with this. This is not a problem. Now: How are you calling your move method? If, for example, you call it like this: move(4); then the move(int) method will be invoked, because your actual parameter (4) is an int. Java checks for the best match in parameter lists. If you rename you method, it all works, because Java will recognise that there is an int parameter, and the only matching method expects a double, and then convert your int to a double. But if there's a method where the int fits without conversion, it'll call that one. So, in short: to ensure that your own method is called, use a double as an argument: move (4.0); That should do it. Michael
Busch2207 Busch2207

2011/6/17

#
Ahh Great! Now it works! Thank you very much! :)
You need to login to post a reply.