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

2014/3/20

"counter.add(1);" refuses to work

H8_2_BeThatGuy H8_2_BeThatGuy

2014/3/20

#
"counter.add(1);" refuses to work in my program and I have no idea why. please help
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class EvilWombat here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class BlueCube extends Actor
{
    /**
     * Act - do whatever the EvilWombat wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(-9);
        checkKill(); 
       if(getX() < 10)
       {
           counter.add(1);
       }
        Remove();
   }
       
   private Counter counter;
   public BlueCube (Counter pointCounter)
   {
       counter = pointCounter;
    }
   
   
   
      public void checkKill()
    {
        if(canSee(Soldier1.class))
        {
            eat(Soldier1.class);
            Greenfoot.stop();
        }
    }
    
    
    
     public boolean canSee(Class clss)
    {
      Actor actor = getOneObjectAtOffset(0, 0, clss);
      return actor != null;
    }



     public void eat(Class clss)
    {
      Actor actor = getOneObjectAtOffset(0, 0, clss);
      if(actor != null) {
        getWorld().removeObject(actor);
      }
    }
    
    public void Remove()
   {
       if(getX() < 10)
       {
           getWorld().removeObject(this);
       }
   }
}    
danpost danpost

2014/3/20

#
I think it works. It is just that at the same time it works, you are removing the BlueCube actor from the world and the counter object goes bye-bye with it. Is the BlueCube object being replaced or are there multiple BlueCube objects in the world? Are different BlueCube objects given the same Counter objects or are new ones being created for each one?
H8_2_BeThatGuy H8_2_BeThatGuy

2014/3/21

#
There are multiple BlueCubes being generated at once. As for the second question: There is one counter object. There are multiple BlueCubes that continually spawn. ( It is a sort of dodge ball game) All BlueCubes that are spawned are linked to the one counter.
danpost danpost

2014/3/21

#
Will have to see all code related to the counter in your world subclass.
You need to login to post a reply.