0

I am trying to create a basic Snake game. I have a very basic 2D Java Game Engine.Here is how all the Update's and Rendering happen:

I have an ArrayList of GameObjects which is a class I created and has two abstract methods called "update" and "render" (And also some variables which have nothing to do with the question but telling it may decrease confusion when answering it). I create objects inside the game class like in the example code below and then add these to the ArrayList. Then I basically use an enhanced for loop to update and render all the objects in the ArrayList.

I basically want to create an object in a method while the game is running (A body part will be created everytime a "food" is eaten).

When I use the following code to create a body part inside a method, I get an error for each variable I use in the "render" part saying

Cannot refer to a non-final variable body inside an inner class defined in a different method

GameObject body = new GameObject(DOT_SIZE, DOT_SIZE, food.boardPosX, food.boardPosY) {

    @Override
    void update() { 
    //MOVE PART
    }

    @Override
    void render(Graphics g) {
        g.fillRect(body.pixelPosX, body.pixelPosY, body.pixelSizeX, body.pixelSizeY);
    }
};

Also If I don't put anything into the rendering method and basically leave everything empty and try to add this "head" to the ArrayList of GameObjects in the same method I get the java.util.ConcurrentModificationException exception.

Further information:

  • The objects which exist from the very beginning (the head and the food) are;

    • created in the Game class (and not in any kind of methods)
    • added to the ArrayList of GameObjects in a method called in the constructor of the Game class.

EDIT - More Information:

  • When I make body final, I get an error saying The local variable body may not have been initialized.

The question is:

  • Where and how should I define the "body" object.
  • Where and how should I add it to the ArrayList of objects.

Thanks for your consideration.

2
  • You wrote an anonymous class, assigns the new instance to body, and then you use your just created body instance in your overridden render method, which is not correct. Commented May 9, 2014 at 21:27
  • Only the "render" and "update" methods are abstract. Would that still be called an anonymous class? Also I understand that it does not matter since the parts I use are abstract. So what is the right way to do it? Commented May 9, 2014 at 21:31

1 Answer 1

1
When I make body final, I get an error saying The local variable
body may not have been initialized.  

When you declare a variable as final, you have to assign it a value right away, or assign a value in the constructor, or you will get an error.

I suggest you to separate the GameObject with Graphics, modify Graphics object after GameObject object has been initialized at least.

For GameObject, implements render() and update in a child class(Body), you can invoke it each time your body object is created. For example, make your GameObject class as an abstract class, keep those two methods abstract, create class Body to extends from GameObject and implements them.

Based on the following code you provided,

    void render(Graphics g) {
        g.fillRect(body.pixelPosX, body.pixelPosY, body.pixelSizeX, body.pixelSizeY);
    }

I guess you wanna something like this :

    void render(Graphics g) {
                g.fillRect(this.pixelPosX, this.pixelPosY, this.pixelSizeX, this.pixelSizeY);
   }

You tried to assign values of current object to pass into fillRect

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.