Hi, I'm not sure if the header has the correct terminology, but basically what I want to do is create multiple new objects of a class with different names so that they can be differentiated.
I have a class (Guard) that needs to create 4 objects of the class NPCSensor with the names "NPCS1" "NPCS2" "NPCS3" and "NPCS4" they are created above, below and to the left and right of the guard. I had an attempt at the method, but it kept asking for brackets.
Here is my code:
import greenfoot.*;
/**
* Write a description of class Guard here.
*
* @author h123n
* @version (1.2)
*/
public class Guard extends NPC
{
public boolean follow = false;
private Toby toby;
public int distancetargetx = 10;
public int distancetargety = 10;
/**
* Act - do whatever the Guard wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
speed = 7;
damage = 20;
health = 70;
name = "Officer";
if (toby != null)
{
World world;
world = getWorld();
//this should be 1
world.addObject(new NPCSensor(), (this.getGlobalX()), (this.getGlobalY() - 50));
//this should be 2
world.addObject(new NPCSensor(), (this.getGlobalX()), (this.getGlobalY() + 50));
//this should be 3
world.addObject(new NPCSensor(), (this.getGlobalX() - 50), (this.getGlobalY()));
//this should be 4
world.addObject(new NPCSensor(), (this.getGlobalX() + 50), (this.getGlobalY()));
}
}
public void follow(Toby person) {
toby = person;
}
}