-1

I am trying to call a variable from a private method into another method in the same class, but on chicken.x i get the error:

chicken cannot be resolved to a variable

How can I call the sprite rectangle out of the private method?

Code:

public class OptionScreen implements Screen{
    ...
    Array<Rectangle> chickens;
    ...

    public OptionsScreen(){
        ...
        chickens = new Array<Rectangle>();
        ...
    }

    private void spawnChicken(){
        Rectangle chicken = new Rectangle();
        ...
        chickens.add(chicken);
        ...
    }

    public void render(float delta){
        ...
        diffYchick = (float) (farmerY - chicken.y); // Error here
        ...
    }
}
Andrew_CS
  • 2,542
  • 1
  • 18
  • 38
user3165683
  • 347
  • 1
  • 9
  • 28
  • What class is `method` in and what class is `render` in? – Andrew_CS Jul 15 '14 at 18:06
  • @Andrew_CS both in same OptionScreen class, chicken is the actual name for the sprite – user3165683 Jul 15 '14 at 18:14
  • Still not sure what you mean with `chicken`. I've updated my answer. Are you trying to call `render` in the `main` method of the `OptionScreen` class? – Andrew_CS Jul 15 '14 at 18:19
  • @Andrew_CS sprite takes the place of chicken in my game – user3165683 Jul 15 '14 at 18:22
  • You need to show the code that contains `chicken` if neither answer here has solved your issue. – Andrew_CS Jul 15 '14 at 18:27
  • @Andrew_CS diffXchick = (float) (farmerX - chicken.x); – user3165683 Jul 15 '14 at 18:29
  • 1
    What is the type of `chicken`? Where are you initializing it? I tried to chat with you to fix your problem but you didn't respond. You're making it difficult for people to help you. – Andrew_CS Jul 15 '14 at 18:45
  • @Andrew_CS 1st how do i use the chat? here is some more info Array chickens; chickens = new Array(); pls c http://stackoverflow.com/questions/24763379/controlling-and-creating-multiple-sprites-array-java-libgdx?noredirect=1#comment38425579_24763379 another question which should provide more info – user3165683 Jul 15 '14 at 18:54
  • Follow this link to chat - http://chat.stackoverflow.com/rooms/57343/discussion-between-andrew-cs-and-user3165683 – Andrew_CS Jul 15 '14 at 19:03
  • @Andrew_CS pls c edit – user3165683 Jul 15 '14 at 19:03

2 Answers2

1

After Chat To answer the underlying problem that was discussed in chat:

There was an ArrayList<Rectangle> chickens as an instance variable for the OptionsScreen class. The OP wasn't getting the Rectangle's out of the ArrayList in the render method. OP was using a variable name chicken from a different method, spawnChicken that only had method scope. I showed the OP how to loop over the ArrayList in the render method, access the Rectangles one at a time, and then perform the necessary operations.


From Before Chat And Edited Question The scope of sprite is only in the method method. This means that you can only make use of the sprite variable while inside of method. You could make sprite an instance variable, then you could access it in another method of that same class.

public class OptionScreen{
    private Rectangle sprite; // Make instance variable

    public OptionScreen(){ // Constructor - called when initializing
        sprite = new Rectangle(); // Initialize (so it's not null)
        ... // Set other info on sprite, such as x
    }

    public void render(float delta){
        ...
        sprite.x;
        ...
    }

    public static void main(String[] args){ // Example of useage
        OptionScreen optionScreen = new OptionScreen(); // Constructor called
        optionScreen.render(1.1);
    }
}

Initializing sprite in a constructor ensures that it won't be null when attempting to use render. (Unless you set it to null)


Note: It is probably a good idea to not name methods method since it gets confusing.

Andrew_CS
  • 2,542
  • 1
  • 18
  • 38
0

If you define sprite locally in one method, you cannot access it within another method.

Not sure about the chicken though (there is no such variable in the code you posted).

If sprite must be accessed by a different method, make it an instance variable.

public class ClassName 
{
  private Rectangle _sprite = null;

  ...

  private void method(){
     _sprite = new Rectangle();
  }

  ...

  public void render(float delta) {
    if (_sprite != null) {
      _sprite.x ...
    }
  }

}
Eran
  • 387,369
  • 54
  • 702
  • 768