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

2015/4/18

Create an object with instance?

h123n h123n

2015/4/18

#
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;
    }   
}
jdf1234567 jdf1234567

2015/4/18

#
So what exactly do u intend to do and why do u set speed, damage, heath, name every act?
h123n wrote...
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())); } }
I'm not sure but is it possible that u canged the constructor and the act()? Could u pls give us the errormessage?
jdf1234567 jdf1234567

2015/4/18

#
And by the way you can easily safe a reference to the objects by
NPCSensor NPCS1=new NPCSensor();
world.addObject(NPCS1, x, y);
h123n h123n

2015/4/18

#
at the moment, the code works, so there is no error message. Health, damage, speed etc is just there for no reason really. When the NPC's are fully programmed, the attributes will do as they say. by the way, all attributes are declared in the NPC class.
h123n h123n

2015/4/18

#
thanks jdf1234567! that's exactly the code I was looking for!
You need to login to post a reply.