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

2022/6/1

moving to an actor without turning

Mauckenweg Mauckenweg

2022/6/1

#
Im working on a project for scool and i need an enemy to be able to run towards the hero (maybe with diffrent speeds). I have no idea how to do it since im bad at coding... Please help.
danpost danpost

2022/6/1

#
Mauckenweg wrote...
moving to an actor without turning
"without turning" is vague. That is -- what exactly are you experiencing as far the the behavior? (when is it turning, how is it turning) I can only assume that you are talking about your actor is maybe doing something like the following -- moving horizontally for a while, then moving diagonally (or vertically), to get to the hero. In that case, you should use a smooth moving engine -- either the SmoothMover class provided by greenfoot or my QActor (Quality Actor) class. Both provided a way to control the movement of an actor so that it moves smoothly in any general direction. My QActor class can be found in my Asteroids w/Improved QActor class scenario.
Mauckenweg Mauckenweg

2022/6/2

#
I kinda thought of it as it first getting the coordinates of the hero ,then moving to those coordinates using setLocation
danpost danpost

2022/6/2

#
"without turning" is vague. If you are talking about not having the enemy show any rotation while moving toward the player, then save the "rotation" (direction moving) in a field. Set the rotation before moving and reset it back to zero after moving:
// with a field
private int rotation;
// the following act format
public void act()
{
    setRotation(rotation);
    // all moving codes here
    //
    // the following at end of act
    rotation = getRotation();
    setRotation(0);
}
With the above, you can use something like the following for moving:
java.util.List playerList = getWorld().getObjects(Player.class)
if ( ! playerList.isEmpty())
{
    Actor player = playerList.get(0);
    turnTowards(player.getX(), player.getY());
    move(3);
}
If the player has to be in a certain range of the enemy, then change line 1 above to:
java.util.List playerList = getObjectsInRange(200, Player.class); // adjust range as needed
Mauckenweg Mauckenweg

2022/6/2

#
Sorry for wasting your time I figured my self...
public void act() 
    {
        follow();
    }
    public void follow()
    {
        trackX();
        trackY();
    }
    public void trackX()
    {
        { 
        World world;
        world=getWorld();
        Object obj=world.getObjects(InsertNameHere.class).get(0);
        InsertNameHere doc=(InsertNameHere) obj;
        int InsertNameHereX=doc.getX();
        getWorld().showText(""+InsertNameHereX,50,10);
        
        if (getX() < InsertNameHereX) move(2);
        if (getX() > InsertNameHereX) move(-2);
        }
    }
    public void trackY()
    {
        { 
        World world;
        world=getWorld();
        Object obj=world.getObjects(InsertNameHere.class).get(0);
        InsertNameHere doc=(InsertNameHere) obj;
        int InsertNameHereY=doc.getY();
        getWorld().showText(""+InsertNameHereY,150,10);
        
        if (getY() > InsertNameHereY) setLocation(getX(),getY()-2);
        if (getY() < InsertNameHereY) setLocation(getX(),getY()+2);
        }
    }
Mauckenweg Mauckenweg

2022/6/2

#
But i used code you wrote in another discuss so with out you i wouldnt have been able to do it! THANKS ;)
You need to login to post a reply.