Search code examples
javavariablesfilenamesgreenfoot

filename being a variable


I'm trying to make a variable as a filename. I want a generic command that when inherited by a subclass can then set a filename as the variable.

The code compiles just fun but when I run it and I press the d or a key to move the avatar I get a popupwindow saying Could not find file: avatarRight

Edit:If I remove the quotes from the parameter list and initialize the filename above then it runs but I want to be able to initialize the variable in the subclass so that multiple subclasses can have different images

Superclass method:

/**
* Sets up the movement keys and facing for the Object
*/
public void movement()
{
     String avatarRight = "Alien.png";
     String avatarLeft = "Alien1.png";

     if (atWorldEdge() == false)
      {
        if (Greenfoot.isKeyDown("w"))
         {
            setLocation(getX(), getY()-1);
         }
        if (Greenfoot.isKeyDown("d"))
         {
            setImage(avatarRight);
            setLocation(getX()+1, getY());
         }
        if (Greenfoot.isKeyDown("s"))
         {
            setLocation(getX(), getY()+1);
         }
        if (Greenfoot.isKeyDown("a"))
         {
            setImage(avatarLeft);
            setLocation(getX()-1, getY());
         }
      }
     else
      {
      }
}

Subclass:

public class Alien extends Living
{
   private String avatarRight = "Alien.png";
   private String avatarLeft = "Alien1.png";
   /**
     * Act - do whatever the Alien wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
   public void act() 
    {
      movement();
    }    
}

Solution

  • Instead of declaring the image variables inside the method movement, instead declare the one you want to use inside your class and pass that variable to the method that you want to use it.

    So instead of doing this:

    public void movement()
    {
     String avatarRight = "Alien.png";
     String avatarLeft = "Alien1.png";
    ...
    

    and this:

    public class Alien extends Living
    {
      private String avatarRight = "Alien.png";
      private String avatarLeft = "Alien1.png";
      /**
     * Act - do whatever the Alien wants to do. This method is called     whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
     public void act() 
     {
       movement();
     }    
    }
    

    Do this:

    public void movement(String avatarLeft, String avatarRight)
    {
    ...
    

    and this:

    public class Alien extends Living
    {
      private String avatarRight = "Alien.png";
      private String avatarLeft = "Alien1.png";
    /**
    * Act - do whatever the Alien wants to do. This method is called           whenever
    * the 'Act' or 'Run' button gets pressed in the environment.
    */
    public void act() 
    {
       movement(avatarLeft, avatarRight);
    }    
    }
    

    This will allow you to pass different images to the movement method from the different avatar classes.