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

2015/5/10

how do i make an actor class into an arraylist.

5mark7 5mark7

2015/5/10

#
I want to have specific control of each object in my actor subclass. For example, make a class called Crab. In the field, create a List crabTotal, which I initialized in the construtor to: crabTotal=arrayList<Crab>(); My goal is to keep track of how many ants each crab object eats. So, then I try say doing this: Crab cr1=crabTotal.get(1); I have done this sort of thing in Processing and Java, but am confused why it won't work here.
danpost danpost

2015/5/10

#
What have you got, so far? Show your World and Crab class codes (read the 'posting code' link below the reply box before posting).
5mark7 5mark7

2015/5/11

#
/*Ok, I really appreciate your time. I'm new to this and simultaneously trying to "volunteer" teach it to some high school students during my lunch period. Unfortunately, since I lasted talked, I changed it all around and tried to use an array from the World class. It runs fine till I try to "truly" run the array with more than one object. My goal is to be able to have direct control of each crab actor individually. So, I've included my Crab (actor) class and my world class. thanks for taking the time. if this isn't feasible, or either way, it seems like the arraylist would be the better solution. */ //ummm, sorry, can't seem to get the code thing to work either.

public class Crab extends Actor
{
    
    //ArrayList<Ants> capTure;
   //public List crabTotal;
    List capTure;
    List ladyBug;
   // public Crab[] crabarray;
    public static int antcount;
    public int ants_crab;
    public Crab(){
        capTure=new ArrayList<Ants>();//crabTotal=new ArrayList<Crab>();
        ladyBug=new ArrayList<Lady>();
          antcount=0;ants_crab=0;

    }

    public void act()
    {
        run1(); capAnts();capLadyBug();crabCount();
    }

    public void run1(){if ( isAtEdge() ) 
        {
            turn(30);setLocation(getWorld().getWidth()-getX(),getWorld().getHeight()-getY());
        }
        if ( Greenfoot.getRandomNumber(100) < 10 ) 
        {
            turn( Greenfoot.getRandomNumber(20)-10 );
        }
        move(5);
    }

    public void capAnts(){
        capTure= getObjectsInRange(10,Ants.class);//Crab.antcount=Crab.antcount+capTure.size();
        for(int j=0;j<1;j++)//CrabWorld.crabArray.length;j++)
        {CrabWorld.crabArray[j].ants_crab=CrabWorld.crabArray[j].ants_crab+capTure.size();}
        getWorld().removeObjects(capTure);
        if(getWorld().numberOfObjects()<20){ int ct=50;for(int j=1;j<ct;j++){Ants a=new Ants();
                getWorld().addObject(a,getWorld().getWidth()/2,200);}
        }   
    }

    public void capLadyBug(){
        if(isTouching(Lady.class)){removeTouching(Lady.class);}
        //if(getWorld().numberOfObjects()>=1){removeObjects(ladyBug);}    
    }   

    public void crabCount(){
        int xc=0; 
        //for(int j=0;j<crabTotal.size();j++){
           // Crab cr1=crabTotal.get(1);int c=cr1.ants_crab;
           getWorld().showText("number of ants eaten by ",30,50);
           for(int j=0;j<1;j++){//CrabWorld.crabArray.length;j++){
           int c=CrabWorld.crabArray[j].ants_crab;
            String num=Integer.toString(c);String js=Integer.toString(j);   
           getWorld().showText(js+ "crab is "+num,30,50+20*j);}  
        //}
    }

    
}
//now the world class

public class CrabWorld extends World
{
    
    public static Crab[] crabArray=new Crab[5];
    public CrabWorld() 
    {
        super(560, 560, 1);
        for(int j=0;j<crabArray.length;j++){
        crabArray[0]=new Crab();
        addObject(crabArray[0],50+30*j,50+30*j);}
        prepare();
    }

    
    private void prepare()
    {
        /*Crab crab = new Crab();
        addObject(crab, 357, 118);
        Crab crab2 = new Crab();
        addObject(crab2, 211, 223);
        Crab crab3 = new Crab();
        addObject(crab3, 360, 367);
        Crab crab4 = new Crab();
        addObject(crab4, 427, 242);*/
        Ants ants = new Ants();
        addObject(ants, 321, 203);
        Ants ants2 = new Ants();
        addObject(ants2, 229, 256);
        Ants ants3 = new Ants();
        addObject(ants3, 124, 239);
        Ants ants4 = new Ants();
        addObject(ants4, 197, 376);
        Ants ants5 = new Ants();
        addObject(ants5, 396, 503);
        Ants ants6 = new Ants();
        addObject(ants6, 444, 359);
        Ants ants7 = new Ants();
        addObject(ants7, 191, 175);
        Ants ants8 = new Ants();
        addObject(ants8, 108, 114);
        Ants ants9 = new Ants();
        addObject(ants9, 198, 89);
        Ants ants10 = new Ants();
        addObject(ants10, 53, 320);
        ants10.setLocation(52, 339);
        Ants ants11 = new Ants();
        addObject(ants11, 52, 340);
        Ants ants12 = new Ants();
        addObject(ants12, 42, 377);
        Ants ants13 = new Ants();
        addObject(ants13, 69, 402);
        Ants ants14 = new Ants();
        addObject(ants14, 70, 403);
        Ants ants15 = new Ants();
        addObject(ants15, 59, 425);
        Ants ants16 = new Ants();
        addObject(ants16, 59, 425);
        Ants ants17 = new Ants();
        addObject(ants17, 57, 419);
        Ants ants18 = new Ants();
        addObject(ants18, 55, 421);
        Ants ants19 = new Ants();
        addObject(ants19, 53, 425);
        Ants ants20 = new Ants();
        addObject(ants20, 53, 427);
        Ants ants21 = new Ants();
        addObject(ants21, 42, 442);
        Ants ants22 = new Ants();
        addObject(ants22, 42, 452);
        Ants ants23 = new Ants();
        addObject(ants23, 42, 454);
        Ants ants24 = new Ants();
        addObject(ants24, 47, 457);
        Ants ants25 = new Ants();
        addObject(ants25, 60, 463);
        Ants ants26 = new Ants();
        addObject(ants26, 73, 466);
        Ants ants27 = new Ants();
        addObject(ants27, 84, 469);
        Ants ants28 = new Ants();
        addObject(ants28, 98, 469);
        Ants ants29 = new Ants();
        addObject(ants29, 104, 469);
        Ants ants30 = new Ants();
        addObject(ants30, 105, 465);
        Ants ants31 = new Ants();
        addObject(ants31, 105, 446);
        Ants ants32 = new Ants();
        addObject(ants32, 94, 435);
        Ants ants33 = new Ants();
        addObject(ants33, 90, 440);
        Ants ants34 = new Ants();
        addObject(ants34, 85, 457);
        Ants ants35 = new Ants();
        addObject(ants35, 79, 459);
        Ants ants36 = new Ants();
        addObject(ants36, 78, 454);
        Ants ants37 = new Ants();
        addObject(ants37, 80, 430);
        Ants ants38 = new Ants();
        addObject(ants38, 81, 425);
        Ants ants39 = new Ants();
        addObject(ants39, 90, 412);
        Ants ants40 = new Ants();
        addObject(ants40, 90, 358);
        Ants ants41 = new Ants();
        addObject(ants41, 97, 356);
        Ants ants42 = new Ants();
        addObject(ants42, 257, 332);
        Ants ants43 = new Ants();
        addObject(ants43, 274, 329);
        Ants ants44 = new Ants();
        addObject(ants44, 307, 306);
        Ants ants45 = new Ants();
        addObject(ants45, 307, 306);
        Ants ants46 = new Ants();
        addObject(ants46, 272, 309);
        Ants ants47 = new Ants();
        addObject(ants47, 281, 320);
        Ants ants48 = new Ants();
        addObject(ants48, 300, 322);
        Ants ants49 = new Ants();
        addObject(ants49, 305, 332);
        Ants ants50 = new Ants();
        addObject(ants50, 299, 339);
        Ants ants51 = new Ants();
        addObject(ants51, 296, 340);
        Lady lady = new Lady();
        addObject(lady, 336, 252);
        lady.setLocation(192, 472);
    }
}
danpost danpost

2015/5/11

#
You biggest problem is located at lines 73 and 74, where you are using the index of zero for all the newly created crabs. You end up with the fifth crab at index zero and all the other elements in your array maintain a 'null' value. Replace
crabArray[0]
// with
crabArray[j]
5mark7 5mark7

2015/5/11

#
Hi Dan.. I hope you skip the previous post. I see where I had a major error in my world class. It all works fine except that I need to figure out how to empty my ant (actors) arraylist after each "eating." I've re-posted my two corrected classes below. If you have any advice on the issue of emptying my ant arraylist after each eating, I'd love to hear it. Now that I have this working, I am going to try and redo it using only an arraylist for the crabs in their own class. hence, i'll probably be back soon.

public class Crab extends Actor
{

    //ArrayList<Ants> capTure;
    //public List crabTotal;
    List capTure;
    List ladyBug;
    // public Crab[] crabarray;
    public static int antcount;
    public int ants_crab;
    public Crab(){
        capTure=new ArrayList<Ants>();//crabTotal=new ArrayList<Crab>();
        ladyBug=new ArrayList<Lady>();
        antcount=0;ants_crab=0;

    }

    public void act()
    {
        run1(); capAnts();capLadyBug();crabCount();
    }

    public void run1(){if ( isAtEdge() ) 
        {
            turn(30);setLocation(getWorld().getWidth()-getX(),getWorld().getHeight()-getY());
        }
        if ( Greenfoot.getRandomNumber(100) < 10 ) 
        {
            turn( Greenfoot.getRandomNumber(20)-10 );
        }
        move(5);
    }

    public void capAnts(){
        //capTure= CrabWorld.crabArray[j].getObjectsInRange(10,Ants.class);//Crab.antcount=Crab.antcount+capTure.size();
        for(int j=0;j<CrabWorld.crabArray.length;j++)
        {capTure= CrabWorld.crabArray[j].getObjectsInRange(10,Ants.class);
            CrabWorld.crabArray[j].ants_crab=CrabWorld.crabArray[j].ants_crab+capTure.size();}
        getWorld().removeObjects(capTure);
        if(getWorld().numberOfObjects()<20){ int ct=50;for(int j=1;j<ct;j++){Ants a=new Ants();
                getWorld().addObject(a,getWorld().getWidth()/2,200);}
        }   
    }

    public void capLadyBug(){
        if(isTouching(Lady.class)){removeTouching(Lady.class);}
        //if(getWorld().numberOfObjects()>=1){removeObjects(ladyBug);}    
    }   

    public void crabCount(){
        int xc=0; 
        //for(int j=0;j<crabTotal.size();j++){
        // Crab cr1=crabTotal.get(1);int c=cr1.ants_crab;
        getWorld().showText("number of ants eaten by ",30,50);
        for(int j=0;j<CrabWorld.crabArray.length;j++){
            int c=CrabWorld.crabArray[j].ants_crab;
            String num=Integer.toString(c);String js=Integer.toString(j);   
            getWorld().showText(js+ "crab is "+num,30,50+20*j);}  
        //}
    }

}

public class CrabWorld extends World
{
    
    public static Crab[] crabArray=new Crab[5];
    public CrabWorld() 
    {
        super(560, 560, 1);
        for(int j=0;j<crabArray.length;j++){
        crabArray[j]=new Crab();
        addObject(crabArray[j],50+30*j,50+30*j);}
        prepare();
    }

    
    private void prepare()
    {
        /*Crab crab = new Crab();
        addObject(crab, 357, 118);
        Crab crab2 = new Crab();
        addObject(crab2, 211, 223);
        Crab crab3 = new Crab();
        addObject(crab3, 360, 367);
        Crab crab4 = new Crab();
        addObject(crab4, 427, 242);*/
        Ants ants = new Ants();
        addObject(ants, 321, 203);
        Ants ants2 = new Ants();
        addObject(ants2, 229, 256);
        Ants ants3 = new Ants();
        addObject(ants3, 124, 239);
        Ants ants4 = new Ants();
        addObject(ants4, 197, 376);
        Ants ants5 = new Ants();
        addObject(ants5, 396, 503);
        Ants ants6 = new Ants();
        addObject(ants6, 444, 359);
        Ants ants7 = new Ants();
        addObject(ants7, 191, 175);
        Ants ants8 = new Ants();
        addObject(ants8, 108, 114);
        Ants ants9 = new Ants();
        addObject(ants9, 198, 89);
        Ants ants10 = new Ants();
        addObject(ants10, 53, 320);
        ants10.setLocation(52, 339);
        Ants ants11 = new Ants();
        addObject(ants11, 52, 340);
        Ants ants12 = new Ants();
        addObject(ants12, 42, 377);
        Ants ants13 = new Ants();
        addObject(ants13, 69, 402);
        Ants ants14 = new Ants();
        addObject(ants14, 70, 403);
        Ants ants15 = new Ants();
        addObject(ants15, 59, 425);
        Ants ants16 = new Ants();
        addObject(ants16, 59, 425);
        Ants ants17 = new Ants();
        addObject(ants17, 57, 419);
        Ants ants18 = new Ants();
        addObject(ants18, 55, 421);
        Ants ants19 = new Ants();
        addObject(ants19, 53, 425);
        Ants ants20 = new Ants();
        addObject(ants20, 53, 427);
        Ants ants21 = new Ants();
        addObject(ants21, 42, 442);
        Ants ants22 = new Ants();
        addObject(ants22, 42, 452);
        Ants ants23 = new Ants();
        addObject(ants23, 42, 454);
        Ants ants24 = new Ants();
        addObject(ants24, 47, 457);
        Ants ants25 = new Ants();
        addObject(ants25, 60, 463);
        Ants ants26 = new Ants();
        addObject(ants26, 73, 466);
        Ants ants27 = new Ants();
        addObject(ants27, 84, 469);
        Ants ants28 = new Ants();
        addObject(ants28, 98, 469);
        Ants ants29 = new Ants();
        addObject(ants29, 104, 469);
        Ants ants30 = new Ants();
        addObject(ants30, 105, 465);
        Ants ants31 = new Ants();
        addObject(ants31, 105, 446);
        Ants ants32 = new Ants();
        addObject(ants32, 94, 435);
        Ants ants33 = new Ants();
        addObject(ants33, 90, 440);
        Ants ants34 = new Ants();
        addObject(ants34, 85, 457);
        Ants ants35 = new Ants();
        addObject(ants35, 79, 459);
        Ants ants36 = new Ants();
        addObject(ants36, 78, 454);
        Ants ants37 = new Ants();
        addObject(ants37, 80, 430);
        Ants ants38 = new Ants();
        addObject(ants38, 81, 425);
        Ants ants39 = new Ants();
        addObject(ants39, 90, 412);
        Ants ants40 = new Ants();
        addObject(ants40, 90, 358);
        Ants ants41 = new Ants();
        addObject(ants41, 97, 356);
        Ants ants42 = new Ants();
        addObject(ants42, 257, 332);
        Ants ants43 = new Ants();
        addObject(ants43, 274, 329);
        Ants ants44 = new Ants();
        addObject(ants44, 307, 306);
        Ants ants45 = new Ants();
        addObject(ants45, 307, 306);
        Ants ants46 = new Ants();
        addObject(ants46, 272, 309);
        Ants ants47 = new Ants();
        addObject(ants47, 281, 320);
        Ants ants48 = new Ants();
        addObject(ants48, 300, 322);
        Ants ants49 = new Ants();
        addObject(ants49, 305, 332);
        Ants ants50 = new Ants();
        addObject(ants50, 299, 339);
        Ants ants51 = new Ants();
        addObject(ants51, 296, 340);
        Lady lady = new Lady();
        addObject(lady, 336, 252);
        lady.setLocation(192, 472);
    }
}

5mark7 5mark7

2015/5/11

#
dang, i missed your post of 5 minutes ago till just now. thank you
danpost danpost

2015/5/11

#
You can either assign a new arraylist to the field:
capTure = new ArrayList<Ants>();
or clear the list with:
capTure.clear();
You will have to be more specific as to what you mean by "after each eating". What conditions determine when an eating period starts and ends?
5mark7 5mark7

2015/5/11

#
thank you for the capTure.clear(); what I mean by eating is removing ants as they by using getObjectsInRange() method and then removing the list. But as I watch this game closely, only one of the crabs in the array of crabs is actually removing ants. My method is shown below.
 public void capAnts(){
        //capTure= CrabWorld.crabArray[j].getObjectsInRange(10,Ants.class);//Crab.antcount=Crab.antcount+capTure.size();
        for(int j=0;j<2;j++)//CrabWorld.crabArray.length;j++)
        {capTure= CrabWorld.crabArray[j].getObjectsInRange(20,Ants.class);
            CrabWorld.crabArray[j].ants_crab=CrabWorld.crabArray[j].ants_crab+capTure.size();}
        getWorld().removeObjects(capTure);capTure.clear();
        if(getWorld().numberOfObjects()<20){ int ct=50;for(int j=1;j<ct;j++){Ants a=new Ants();
                getWorld().addObject(a,getWorld().getWidth()/2,200);}
        }   
    }

danpost danpost

2015/5/11

#
It seems you are not understanding something here. Each act cycle consists of the act method of the active world executing along with the act methods of all actors in that world. If you have five crabs in the world, the Crab class act method will execute five times, once with each different crab in the world as the object of the method. Also, when each crab is created, it is given its own instance fields (those that are not declared as static) to work with. The fields of one crab can contain different values from those of another. Just with that information, you could start your Crab class as follows:
import greenfoot.*;

public class Crab extends Actor
{
    private int antCount;
    private int bugCount;

    public void act()
    {
       for (Object obj : getObjectsInRange(20, Ants.class))
       {
            Actor ant = (Actor) obj;
            getWorld().removeObject(ant);
            antCount++;
        }
        for (Object obj : getObjectsInRange(20, Lady.class))
        {
            Actor bug = (Actor) obj;
            getWorld().removeObject(bug);
            bugCount++;
        }
    }

    public int getAntCount()
    {
        return antCount;
    }

    public int getBugCount()
    {
        return bugCount;
    }
}
With this class code, each crab created will have its own count of how many Lady and Ant object it ate. The getter methods at the end can be used by the world class to get the value of the fields from each Crab object. The world act method is where you should check for how many actors are in the world and add more if needed.
5mark7 5mark7

2015/5/11

#
Dan Thank you very much. That does clear many things up. I am very grateful. I am not a programmer. I actually teach high school math at a small high school, but definitely see the need for programming in schools. We do not have the resources for such a class, so, I'm trying to find something that with some guidance, the students can work on more independently. I have taught them some C++ via processing, but wanted something more object oriented and independent. Hence, I found/stumbled onto Greenfoot recently. It is amazing. so, without further ramblings, thank you. mark.
danpost danpost

2015/5/11

#
You might be interested in the java tutorial trails. The second section on the left titled Trails Covering the Basics would be a good place to start.
You need to login to post a reply.