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

2013/9/4

Programming

Amenya Amenya

2013/9/4

#
can someone help me with the code for getting the position of another actor. I tried the following code but got an error message that Actor not in the world. My code public void act() { // Add your action code here. Cars car=new Cars(); int x= car.getX(); int y=car.getY(); turnTowards(x,y); move(1); }
Gevater_Tod4711 Gevater_Tod4711

2013/9/4

#
The problem is that your car is not added to the world and so it has no x or y coordinates. Thats why getX() and getY() throw this exceptions. You first need to add the car to the world. After creating the object (Car car = new Cars();) you have to add this car to the world using getWorld().addObject(car, x, y); (use the starting coordinates instead of x and y). But I'm not shure if this code acts like you want it to. You wrote this code into the act method and so every act there will be a new car that is added to the world. If you want to follow only one car try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//a global variable car;
private Cars car;
 
public void addedToWorld(World world) {
    //when your actor is added to the world there is a new car also added to the world (only one);
    car = new Cars();
    getWorld().addObject(car, x, y);//again use the real coordinates; not x and y;
}
 
public void act() {
    //the rest of your code;
    turnTowards(car.getX(), car.getY());
    move(1);
}
Amenya Amenya

2013/9/5

#
Thank you very much. The code solved part of my problem. I was able to add the object car in the world. however in the code 10.public void act() { 11. //the rest of your code; 12. turnTowards(car.getX(), car.getY()); 13. move(1); 14.} the 'error java.lang.NullPointerException' is generated at line 12. I am new in programming and trying to understand the syntax of OOP. can you help me overcome this challange
Gevater_Tod4711 Gevater_Tod4711

2013/9/5

#
This error probably means that car is null when you try to execute this code the first time. This seems a bit strange to me because when your actor is added to the world first the addedToWorld method should be called and then the act method. So the car should have been initialised. However, if you surround line 12 with an if clause to check whether car is null this error should no longer appear:
1
2
3
4
5
6
7
public void act() { 
    //the rest of your code; 
    if (car != null) {
        turnTowards(car.getX(), car.getY()); 
    }
    move(1); 
}
If the error still occours please post the whole stack trace here (stack trace is the text that is printed on your console when you get an error).
danpost danpost

2013/9/5

#
Actually, you probably do not want to even move if there is nothing to move towards:
1
2
3
4
5
6
7
public void act() {
    // some possible code
    if (car != null) {
        turnTowards(car.geX(), car.getY());
        move(1);
    }
}
Amenya Amenya

2013/9/5

#
Well this is my code import greenfoot.*; public class Striker extends Actor { private Cars car; public void addedToWorld(World world) { Cars car=new Cars(); getWorld().addObject(car,3,3); } public void act() { turnTowards(new Cars().getX(),new Cars().getY()); move(1); } } This is what I get on the console java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getX(Actor.java:157) at Striker.act(Striker.java:24) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) please help me fix the problem. I would also want an actor moving to detect that it has reached the edge of the world and possibly turn direction. assist me with the code..
Gevater_Tod4711 Gevater_Tod4711

2013/9/5

#
The problem in this code is that you try to turn towards a Cars object that you create but never add to the world. Try using this code instead of your current code in the act method:
1
2
3
4
5
6
public void act() {   
    if (car != null) { 
        turnTowards(car.getX(), car.getY());   
    
    move(1);   
}
2. To check whether your object is at the edge of the world you can use this mehtod:
1
2
3
4
5
6
7
8
9
public boolean atWorldEdge() {
    if (getX() < 20 || getX() > getWorld().getWidth() - 20) {
        return true;
    }
    if (getY() < 20 || getY() > getWorld().getHeight() - 20) {
        return true;
    }
    return false;
}
In your act method you can use it like this:
1
2
3
4
5
6
public void act() {
    //....
    if (atWorldEdge()) { //make shure you added the method in this class; otherwhile it'll not work.
        turn(180); //move back;
    }
}
davmac davmac

2013/9/6

#
The problem in this code is that you try to turn towards a Cars object that you create but never add to the world.
Actually, the problem is on the line in the addedToWorld method that reads: Cars car=new Cars(); Amenya, you added the "Cars" at the start of this line - the code Gevater_Tod4711 originally posted did not have it. If you remove it, it will solve the error you are getting.
Amenya Amenya

2013/9/6

#
thanks
You need to login to post a reply.