Search code examples
javaobjectlibgdxbox2dinstances

Creating multiple instances in libgdx Box2d


I create an object in , lets say a square and I want to make many copies of square with different coordinates so I can call it with

Square square = new Square(int x, int y)

Inside square I created a method that changes the square color. What I'm trying to do is create multiple instances of square

square = new Square(2, 4);
square = new Square(9, 7);
.
.
.

and when I call square.changeColor() I want all of them to change, what would be the best way of doing this?


Solution

  • Make Color in the square class static.

    What are static variables?

    A simplified explanation of it would be that when you make a change in an Object(Square) of a particular static variable(Color). It will change across all instances of the object (Square).

    More information:

    http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html