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

2013/6/30

Give enemies a target

LuMC91 LuMC91

2013/6/30

#
Hello, i have a certain number of enemies and each one of them have to attack a specific target.. I mean enemy1 to target1, enemy2 to target2, etc.. any idea of how to do this??
danpost danpost

2013/6/30

#
Add a Target typed field to the Enemy class and pass which target each enemy must attack to the constructor of the enemy:
// create objects in world constructor
Target target1 = new Target();
Enemy enemy1 = new Enemy(target1);
Target target2 = new Target();
Enemy enemy2 = new Enemy(target2);
Target target3 = new Target();
Enemy enemy3 = new Enemy(target3);
prepare();
// add objects created into the world in the prepare method

// instance field in Enemy class
Target target;
// constructor of Enemy class
public Enemy(Target attacked)
{
    target = attacked;
    // any other construction code you may have
}
The act method (or a method it calls) in the Enemy class should use 'target' to determine how to proceed.
LuMC91 LuMC91

2013/6/30

#
danpost! thank you very much!! this really helped me a lot!! :)
You need to login to post a reply.