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

2016/10/26

Calling method from another class

Shadow7 Shadow7

2016/10/26

#
I am trying to call a method that is from another class that stores a variable. which i need to get the exact location of another object in the world. i know this is very easy to do but i am just brain dead about hoe to do it right now. do I write my code in the Act of my world or do i write it in my world constructor?
Super_Hippo Super_Hippo

2016/10/26

#
So you need the variable in your world? Save a reference to the object when creating it. Then you can use the methods on this object.
NameOfTheClass object;

//when creating (constructor of called from that):
object = new NameOfTheClass();
addObject(object, 100, 100);

//when you need the x and y coordinate (act method?):
object.getX()
object.getY()
//(obviously no full code, add it to where you need it)
You need to login to post a reply.