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

2015/12/7

cannot find symbol - method addObject (...)

smcgee smcgee

2015/12/7

#
import greenfoot.*;

/**
 * Write a description of class AngelOfDeath here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class AngelOfDeath extends enemy
{
   private GreenfootImage image1;
   private GreenfootImage image2;
    /**
     * Act - do whatever the AngelOfDeath wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        followDoge();
        move(2);
      
        
        if (canSee(player.class))
        {
            eat();
            Greenfoot.playSound("Xscream2.wav");
            addObject (new AoDLose(), 300, 300);
            
            
        }
    }    
    public AngelOfDeath()
    {
        image1=new GreenfootImage("imgres.png");
        image2=new GreenfootImage("the_lich_king.png");
        setImage(image1);
      
       
    }
    public void followDoge()
       {
    
         int dist = 400;
         Actor closest = null;
         if(!getObjectsInRange(dist, player.class).isEmpty())
         {
        for (Object obj: getObjectsInRange(dist, player.class))
        {
            Actor doge = (Actor) obj;
            int dogeDist = (int) Math.hypot(doge.getX() - getX(), doge.getY() - getY());
            if (closest == null || dogeDist< dist)
            {
                closest = doge;
                dist = dogeDist;
            }
        }
        turnTowards(closest.getX(),closest.getY());
        
          }
        }
         public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }
      public void eat()
    {      
        Actor player;
        player = getOneObjectAtOffset(0, 0, player.class); 
        if (player!= null) //Checks if enemy is there 
        {
            World world; 
            world = getWorld(); 
            world.removeObject(player);
            
            
        }   
           
    }

     public void switchImage()
    {
        if (getImage()==image1)
        {
            setImage(image2);
        }
        else
        {
            setImage(image1);
        }
    }
}
smcgee smcgee

2015/12/7

#
smcgee wrote...
import greenfoot.*;

/**
 * Write a description of class AngelOfDeath here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class AngelOfDeath extends enemy
{
   private GreenfootImage image1;
   private GreenfootImage image2;
    /**
     * Act - do whatever the AngelOfDeath wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        followDoge();
        move(2);
      
        
        if (canSee(player.class))
        {
            eat();
            Greenfoot.playSound("Xscream2.wav");
            addObject (new AoDLose(), 300, 300);
            
            
        }
    }    
    public AngelOfDeath()
    {
        image1=new GreenfootImage("imgres.png");
        image2=new GreenfootImage("the_lich_king.png");
        setImage(image1);
      
       
    }
    public void followDoge()
       {
    
         int dist = 400;
         Actor closest = null;
         if(!getObjectsInRange(dist, player.class).isEmpty())
         {
        for (Object obj: getObjectsInRange(dist, player.class))
        {
            Actor doge = (Actor) obj;
            int dogeDist = (int) Math.hypot(doge.getX() - getX(), doge.getY() - getY());
            if (closest == null || dogeDist< dist)
            {
                closest = doge;
                dist = dogeDist;
            }
        }
        turnTowards(closest.getX(),closest.getY());
        
          }
        }
         public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }
      public void eat()
    {      
        Actor player;
        player = getOneObjectAtOffset(0, 0, player.class); 
        if (player!= null) //Checks if enemy is there 
        {
            World world; 
            world = getWorld(); 
            world.removeObject(player);
            
            
        }   
           
    }

     public void switchImage()
    {
        if (getImage()==image1)
        {
            setImage(image2);
        }
        else
        {
            setImage(image1);
        }
    }
}
We are working on editing the wonderful program biochickendemo. I would like when the if (canSee(player.class)) is implemented then three things happen. eat, playsound, and addObject. The first two work just fine but I don't know why the addObject method is not working.
danpost danpost

2015/12/7

#
The 'addObject' method is not a member of the Actor class or any class that these classes extend from. I hope that this is enough of a hint.
smcgee smcgee

2015/12/8

#
I went back to the program to do some more work and try to solve this problem after reading your hint. Unfortunately what I tried still does not work. I guess I have several questions. You write that the the AddObject method is not a member of the Actor class but it is a member of the World class. Anyway I tried several steps to solve this problem. Maybe the problem cannot be solved because the addObject method cannot be placed in an if statement. I reviewed the material in chapters 2 and 4 on addObject, bringing in a new library, putting the addObject method inside of a new method I wrote thinking I could then call the method inside the if statement and many other steps but I guess I am missing your hint. Any more hints? public void addObject(Actor object,int x,int y); addObject (new AoDLose(), 300, 300);
danpost danpost

2015/12/8

#
Hint number two: being the 'addObject' method is a member of the World class, it must be executed on a World object.
smcgee smcgee

2015/12/8

#
public void act() 
    {
        followDoge();
        move(2);
      
        
        if (canSee(player.class))
        {
            eat();
            Greenfoot.playSound("Xscream2.wav");
            
            
            
        }
    }  
OK - so the question remains. How do I add an instance of the AngleOfeath class at the end of the if statement?
smcgee smcgee

2015/12/8

#
danpost wrote...
Hint number two: being the 'addObject' method is a member of the World class, it must be executed on a World object.
OK - so the question remains. How do I add an instance of the AngleOfeath class at the end of the if statement?
Super_Hippo Super_Hippo

2015/12/8

#
getWorld().addObject(new ClassName(/*Parameters if needed*/), x-position, y-position);
smcgee smcgee

2015/12/8

#
Super_Hippo wrote...
getWorld().addObject(new ClassName(/*Parameters if needed*/), x-position, y-position);
You rock Super-Hippo! I wrote the following in the if statement of the AngelOfDeath class: getWorld().addObject(new AngelOfDeath(), 300, 300); So the code, as I understand it calls the World Super Class where the addObject method is located - do I understand it correctly?
Super_Hippo Super_Hippo

2015/12/8

#
I think 'Super Class' is the wrong word. 'getWorld' gets the world in which the actor is (or 'null' if it isn't in a world. Then this code would give an error). 'addObject' is a 'World' method and since the world in which the actor is in, is a subclass of 'World', it can use the 'addObject' method.
danpost danpost

2015/12/8

#
The method 'getWorld' returns the World object that the actor is in (whatever AngelOfDeath object the act method is executing for). The 'addObject' method is then executed on that returned World object. This is similar to how the 'removeObject' method is used in lines 72 through 74.
smcgee smcgee

2015/12/9

#
Super_Hippo wrote...
I think 'Super Class' is the wrong word. 'getWorld' gets the world in which the actor is (or 'null' if it isn't in a world. Then this code would give an error). 'addObject' is a 'World' method and since the world in which the actor is in, is a subclass of 'World', it can use the 'addObject' method.
Dan - thank you for taking the time to help me and explain the code. Suzanna
smcgee smcgee

2015/12/9

#
danpost wrote...
The method 'getWorld' returns the World object that the actor is in (whatever AngelOfDeath object the act method is executing for). The 'addObject' method is then executed on that returned World object. This is similar to how the 'removeObject' method is used in lines 72 through 74.
Super_Hippo wrote...
I think 'Super Class' is the wrong word. 'getWorld' gets the world in which the actor is (or 'null' if it isn't in a world. Then this code would give an error). 'addObject' is a 'World' method and since the world in which the actor is in, is a subclass of 'World', it can use the 'addObject' method.
Super_Hippo - thank you for taking the time to help me and explain the code. Suzanna
You need to login to post a reply.