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

2012/12/5

Getting a position of different Objects

Diphthong Diphthong

2012/12/5

#
Hello to all, I want to get the Position of one object of a class in an object of another class. So I created the void
public int getX_dock()
{
    return getX();
}
in the class with the object of that I want to know the position. The classes name is dock. In the other class I try to get the Position with
int x = dock.getX_dock();
But the compiler says
non-static method getX_dock() cannot be referenced from a static contex
What is the Problem? Thank you very much! greetings
danpost danpost

2012/12/5

#
The compiler is trying to tell you that you need an object of that class to call the method on (you cannot just call it on the class itself, as the class does not have an x or y coordinate; only the objects that are created within that class and that are added to the world will have x and y coordinates). Before I continue, let me say that there is no need for a special method to return the value returned from 'getX()' as it can be called on any object that is in the world from any class. Chances are you only have one instance of a dock object in the world or you would not be trying to code it in the manner you tried above; therefore:
int dockX = ((dock) getWorld().getObjects(dock.class).get(0)).getX();
should be what you need. The World class method 'getObjects' return a list of 'dock.class' objects. The List class method 'get(0)' returns the first element in the list. The casting '(dock)' could have been '(Actor)' as well; but one of the two needs to be there for the Actor class method 'getX()' to work.
danpost danpost

2012/12/5

#
You could break the line down into steps, as follows:
World world;
world=getWorld();
Object obj=world.getObjects(dock.class).get(0);
dock doc=(dock) obj;
int dockX=doc.getX();
and if you import 'java.util.List' and 'java.util.ArrayList', lines 3 and 4 above could be written as:
List<dock>docks=new ArrayList<dock>();
docks=world.getObjects(dock.class);
dock doc;
doc=docks.get(0);
Diphthong Diphthong

2012/12/12

#
Hi danpost, thanks for your answer. That works. :) I have one more question: I want to use a variable that I declared and used in Class 1 in Class 2. I tried much, but I don’t know how to do that. The Google search doesn’t helped me, too. How should it be done? If my variables name ist variable and the class name class I can work with the Variable in class without problems. I want to know the value of variable in class 2. The compiler says that ist doesn’t know the variable. If I write class.variable ist says he doesn’t know class... Thanks for your help! regards Jan
danpost danpost

2012/12/12

#
Is it a static or non-static variable (non-static will not have the 'static' keyword in its declaration)? If non-static, how many objects of class2 are created and in the world? If only one, then in class1 (and I will give the variable an 'int' type, since you did not say):
int value = ((class2) getWorld().getObjects(class2.class).get(0)).getVariableValue();
where you have the following method in class2:
public int getVariableValue()
{
    return variable;
}
You need to login to post a reply.