1

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();
    }    
}
Radiodef
  • 37,180
  • 14
  • 90
  • 125
power5000
  • 25
  • 1
  • 9
  • what problem are you having? – Martin Serrano Apr 02 '15 at 23:29
  • 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 – power5000 Apr 02 '15 at 23:31
  • you should edit your question and put the problem description in it – Martin Serrano Apr 02 '15 at 23:33
  • Am I missing something? How's passing the literal string `"avatarRight"` to `setImage` even compile? – DigitalNinja Apr 02 '15 at 23:39
  • I believe it is looking for a file called avatarRight instead of looking for a variable. so it compiles but when it needs the file it cannot find it... – power5000 Apr 02 '15 at 23:41
  • Okay, so is the actual file Alien.png, and you're trying to pass the String variable `avatarRight` which holds the file name that should be used? – DigitalNinja Apr 02 '15 at 23:44
  • There you go. Your edit makes more sense ;) – DigitalNinja Apr 02 '15 at 23:46
  • Why not just pass the image String variables to `movement` such as `movement(avatarRight, avatarLeft);`, and remove the declarations inside `movement`? – DigitalNinja Apr 02 '15 at 23:47
  • @DigitalNinja yes. I have 3 classes, the superclass Living, the subclass Alien and the subclass Human. both Human and Alien use the method movement but I want one to have the picture Alien.png and one to have the picture Human.png I thought that this would be possible by doing `setImage(variable)` and then initializing what the variable is in the subclass.... – power5000 Apr 02 '15 at 23:50
  • Then I think you could implement my previous comment and inside the human class you'd have a variable `String avatarLeft = "Human.png";` or whatever and when you call `movement` from inside the Human class you pass the variable you want to use inside `movement`, in this case it would be the human avatar. – DigitalNinja Apr 02 '15 at 23:53
  • That worked! Thank you so much. I'm a high school student taking Java online and my teacher doesn't reply to my question... so how is it different putting a variable in the method parameter list then doing so inside it's body? – power5000 Apr 03 '15 at 00:01
  • Glad it works! :) If you declare a variable inside a method body its scope is only inside that method, i.e. only that method knows about that variable. If you pass it to the method then the method uses the variable that was declared and assigned from elsewhere, like from your human and alien classes. After the method is finished it "forgets" about the variable but the class/method it came from doesn't. See [this](https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html) for a better explanation. – DigitalNinja Apr 03 '15 at 00:07
  • I rolled back your last edit. There is no need to indicate through the title a problem is solved. See also [*"Question with no answers, but issue solved in the comments"*](http://meta.stackoverflow.com/a/251598/2891664). – Radiodef Apr 03 '15 at 00:14
  • My apologies, I'm well very new. @DigitalNinja please write a answer stating why the variable didn't work inside the method and I will accept it. – power5000 Apr 03 '15 at 00:20
  • There you go. Thanks! – DigitalNinja Apr 03 '15 at 00:24

1 Answers1

0

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.

DigitalNinja
  • 1,078
  • 9
  • 17