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

2013/3/4

set an actors direction

AD12 AD12

2013/3/4

#
i want an actor to face another one, so that he can move towards him just with the method move(1); i dont know how to adjust this... I hope you can help me
AD12 AD12

2013/3/4

#
if i work with turnTowards(); the image is turned away (90 degrees) but i can change the rotation. but still my question is how to let him move towards the other object.?
MatheMagician MatheMagician

2013/3/4

#
Have you tried calling the move(1); method. You can adjust it by changing "1" to whatever you want. Also, it would be more helpful if you posting what code you have so far.
danpost danpost

2013/3/5

#
When using 'move(1)', there are only four directions that the actor can move (the four cardinal directions). When using 'move(2)', you only add the four diagonals. When moving in such small steps, it is better to keep track of the actors x and y location with 'double' values and use 'setLocation(int, int)' for the movement. If your actor is facing away from the object with using turnTowards, then your differences in coordinates need to be negated (you should subtract the coordinate of the actor from the coordinate of the object).
AD12 AD12

2013/3/5

#
Thanks for your replies. I think its better to use setLocation() too . But how do i get him to move into the other actors direction ? And how can i make an actor move faster without adjusting the speed or move over two (or more) cells ?
danpost danpost

2013/3/5

#
You can use one of the methods of the Actor class that return an Actor or a list of Actor objects to acquire a reference to the object you want your actor to face and use 'turnTowards' to turn the actor toward that object's location. As far as making your actor move faster, the ways you listed are the only ways.
AD12 AD12

2013/3/5

#
ok thank you. so thats my act method:
public void act() 
    {
        Ship ship= (Ship) getWorld().getObjects(Ship.class).get(0);
        turnTowards(ship.getX(), ship.getY());
        setRotation(getRotation() - 90);
        move(2);
    }    
why the actor turns around my ship if i start the scenario? With this method, the ship is faced, that works so far, but now my actor shall move towards the ship, how can i make him doing this?
danpost danpost

2013/3/5

#
I think the order of lines 5 and 6 need switched.
AD12 AD12

2013/3/6

#
Yes it works, thank you, but how can i make my actor move to another one?
danpost danpost

2013/3/6

#
I do not know what you want. It should be moving toward a Ship object at this point. Please explain more precisely what you want and supply what code you have tried.
AD12 AD12

2013/3/6

#
ok heres my act Method of the Actor which shall move to the object: (its the only method with the constructor)
public DartLaser()
    {
        try
        {
            Ship ship = (Ship) getWorld().getObjects(Ship.class).get(0);
            turnTowards(ship.getX(), ship.getY());
        }
        catch(IndexOutOfBoundsException ex) {}
    }
    
    public void act() 
    {
        X = getX();
        Y = getY();
        move(4);
        if(getY() >= 649)
        {
            getWorld().removeObject(this);
        }
no the actor should face the others direction and move towards this (he doesnt change the direction if the other object moves, but this is right)
AD12 AD12

2013/3/6

#
but it doesnt work, greenfoot gives me a nullPointerException and when i throw this exception the actor just moves to the right side. it doesnt work
danpost danpost

2013/3/6

#
This is because the actor is not placed into the world until after it is constructed. In fact, 'new DartLaser()' creates the object (this is when the constructor is run), then 'addObject' places the object into a world (this is when the value returned from 'getWorld()' changes from 'null' to the world object the actor is placed into and the 'addedToWorld' method is executed; hence your nullPointerException). What you need to do is move the code that is in the constructor to an overriden 'addedToWorld' method that is automatically executed when the object is placed into a world.
public DartLaser()
{
}

public void addedToWorld(World world)
{
    if (!world.getObjects(Ship.class).isEmpty())
    {
        Ship ship = (Ship)world.getObjects(Ship.class).get(0);
        turnTowards(ship.getX(), ship.getY());
    }
}
    
public void act() 
{
    X = getX();
    Y = getY();
    move(4);
    if(getY() >= 649)
    {
        getWorld().removeObject(this);
    }
You need to login to post a reply.