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

2012/1/13

Moving an object to the Player

Oneshot Oneshot

2012/1/13

#
Hey Community, I have one Player, which I move with "w-a-s-d". This Player shoots on Zombies. These Zombies are automatically pointing to the player. Now i also need, that the Zombies automatically move to the Player till the Zombie reached the Player. Does anyone have a snippet for me, how to move the Zombie to the Player? This is the actual source code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

/**
 * Write a description of class Zombie here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Zombie extends Actor
{
    /**
     * Act - do whatever the Zombie wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void pointingTo(Actor o) {  
            setRotation((int)(180*Math.atan2(o.getY()-getY(),o.getX()-getX())/Math.PI));  
    }  
    
    public void act(){  
        Player p = (Player) getWorld().getObjects(Player.class).get(0);  
        pointingTo(p); 
        hingehen();
    }  
}
Best Regards from Germany and have a nice Weekend!
Morran Morran

2012/1/13

#
If you want the zombies to move to the player, you can try something like this:
public void moveToPlayer()
{  
        //get the player
        Player player = (Player) getWorld().getObjects(Player.class).get(0);
        if(getX() < player.getX()) //if I'm to the left of the player
                setLocation(getX() + 5, getY()); //move right by 5 pixels
        if(getX() > player.getX()) //if I'm to the right of the player
                setLocation(getX() - 5, getY()); //move left by 5 pixels
        if(getY() < player.getY()) //if I'm above the player
                setLocation(getY() + 5, getY()); //move down by 5 pixels
        if(getY() > player.getY()) //if I'm beneath the player
                setLocation(getY() - 5, getY()); //move up by 5 pixels\

         //you can change how many pixels you want the zombie to move by.
}
And just put "moveToPlayer();" into your Zombie's "act()" method. Guten Tag!
Duta Duta

2012/1/13

#
Morran wrote...
If you want the zombies to move to the player, you can try something like this:
public void moveToPlayer()
{  
        //get the player
        Player player = (Player) getWorld().getObjects(Player.class).get(0);
        if(getX() < player.getX()) //if I'm to the left of the player
                setLocation(getX() + 5, getY()); //move right by 5 pixels
        if(getX() > player.getX()) //if I'm to the right of the player
                setLocation(getX() - 5, getY()); //move left by 5 pixels
        if(getY() < player.getY()) //if I'm above the player
                setLocation(getY() + 5, getY()); //move down by 5 pixels
        if(getY() > player.getY()) //if I'm beneath the player
                setLocation(getY() - 5, getY()); //move up by 5 pixels\

         //you can change how many pixels you want the zombie to move by.
}
And just put "moveToPlayer();" into your Zombie's "act()" method. Guten Tag!
Or, because they're pointing towards the player, you can just use a method called:
move(3)
For details on this method, see: this link
You need to login to post a reply.