I just master programming, and this question arose: There is a certain class Plane, in which there are 3 textures and you need to choose a random one. And there is the main class Main, in which you need to change this texture at some point. Here is what I have:

public class Plane extends Sprite { private Array<TextureRegion> colorArray; private TextureRegion color; public TextureRegion redPlane; public TextureRegion bluePlane; public TextureRegion greenPlane; public Plane(TextureRegion color) { redPlane = ResourceManager.getInstance().getTextureRegion("planeRed"); greenPlane = ResourceManager.getInstance().getTextureRegion("planeGreen"); bluePlane = ResourceManager.getInstance().getTextureRegion("planeBlue"); colorArray = new Array<TextureRegion>(); this.color = color; } public void randomColor(){ colorArray.addAll(redPlane,greenPlane,bluePlane); color = colorArray.random(); } 

In the main class:

 private Plane plane; ... plane = new Plane(); ... plane.randomColor(); 
  • The question is not very clear - you ask about the constructor, but in your code you have already described the constructor with the parameter. On the other hand, it is not very clear why you need a constructor with a parameter — your task seems to be solved by your randomColor() method. Just what kind of method are you calling: colorArray.random() ? Standard arrays have no such method. If you want to get a random element of the array, you need to do this: java.util.Random rand = new Random(); color = array[rand.nextInt(array.length)] java.util.Random rand = new Random(); color = array[rand.nextInt(array.length)] . rand can be (and usually better) created separately, for example, in the constructor. - m. vokhm
  • If you wanted to ask how to call constructors with a parameter — just like any other method with a parameter: MyClass instance = new MyClass(parameter); - m. vokhm
  • For example, the classic way to open an input text file: BufferedReader reader = new BufferedReader(new FileReader("File.txt")); . Here, the BufferedReader constructor is passed as a parameter an object of the FileReader class, created by new FileReader("File.txt") constructor new FileReader("File.txt") - m. vokhm
  • Yes, the question was not very well formulated, I agree) colorArray.random () works, this is libgdx. Everything works, draws, but it turns out that just when you start a random color appears, but I need at a certain moment. It turns out that I call randomColor () in the Plane class (not shown here, I have already changed the code) and the result is as I described above. And if you call in the main class, I don’t know what to pass to plane = new Plane (!! here !!); - Andrey Stoyanov
  • I still do not understand what you want. You can make a base of the parameter that creates objects with a random color, you can set a specific color when creating it, passing the color to the constructor as a parameter, and you can change the color to a randomly selected one by calling randomColor() when you need. What exactly does not work? - m. vokhm

0