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

2013/10/12

Getting Coordinates from other Class

THELokusta THELokusta

2013/10/12

#
Hi! I want to follow my Shield my Roboter "Robby".So the shield, when it is colleted is on Robby as a kind of protection. My question is now: How do I get the coordinates of my Robby.class? I saw many other discussions but no solution worked for me.So I asked you guys I hope you can help me out! Marius
Gevater_Tod4711 Gevater_Tod4711

2013/10/13

#
To get the coordinates of another object you first have to get a reference to the Robby object. Therefore you should read this Greenfoot Tutorial. When you have got the reference you can easily check where your object is using the getX and getY methods.
THELokusta THELokusta

2013/10/13

#
I've already seen this Tutorial but I have to say that I don't understand this lines:
Tutorial wrote...
private Counter theCounter;

public Space()
{
    super(600, 400, 1);
    addObject(new Rocket(), 300, 200);
    theCounter = new Counter();
    addObject(theCounter, 5, 5);
}
Make sure your code compiles before you continue. Notice that we added a declaration for a variable called "theCounter". This is an instance variable, meaning that we want the keep the value for as long as the world exists, and so it is declared outside the SpaceWorld constructor (but inside the SpaceWorld class). Now, add a method to the Space class to retrieve the value of "theCounter" so it can be accessed by the rocket:
public Counter getCounter()
{
    return theCounter;
}
We can then access the world from the shot, and then call the getCounter method on the world to obtain a reference to the counter; finally, we can call the bumpCount method on the counter reference! The hitAnAsteroid method in the Shot class should then look like this: void hitAnAsteroid()
{
    Space spaceWorld = (Space) getWorld();  // get a reference to the world
    Counter counter = spaceWorld.getCounter();  // get a reference to the counter
    counter.bumpCount(5);
}
Why should i create a new Counter class? Do I need a extra class to store the Coordinate information? Greetings Marius
Gevater_Tod4711 Gevater_Tod4711

2013/10/13

#
You don't realy need a counter in your game. This is just an example in the tutorial. In your case you would need a shield instead of the counter and the clase where you create and save the shield in would be your robot. So your robot always knows where the shield is at the moment. (Or the other way arround when your shield has to know where your robot is)
You need to login to post a reply.