如何将子类类型数组添加至父类GameElement类型二维数组?
Hey Berk! Great question—let's walk through how to get those subclass arrays into your 2D GameElement array. Since all your classes inherit from GameElement, polymorphism is going to be our best friend here. Here's a step-by-step breakdown:
1. Make Sure Your Outer Array is Initialized
First, double-check that your gameElementsArray is properly set up. Since you're using libGDX's Array (not Java's native array), handle this in your GameWorld constructor:
private GameWorld() { // Initialize the outer array that holds all your subclass arrays gameElementsArray = new Array<>(); }
2. Add Subclass Arrays to the 2D Array
You have two straightforward ways to add your subclass arrays, depending on whether you already have existing subclass-specific arrays or want to create them directly as GameElement arrays.
Option A: Create GameElement Arrays Directly
If you're starting from scratch, create each inner array as an Array<GameElement> and populate it with your subclass instances. This works because subclass objects automatically upcast to their parent GameElement type:
// Create an array for Rocks Array<GameElement> rockArray = new Array<>(); rockArray.add(new Rock()); rockArray.add(new Rock()); gameElementsArray.add(rockArray); // Create an array for RockMoving instances Array<GameElement> movingRockArray = new Array<>(); movingRockArray.add(new RockMoving()); movingRockArray.add(new RockMoving()); gameElementsArray.add(movingRockArray); // Repeat this pattern for Powerups, Enemy, PlayerCharacter, etc.
Option B: Convert Existing Subclass Arrays
If you already have arrays typed to your subclasses (like Array<Rock>), you can convert them to Array<GameElement> using libGDX's Array constructor, which accepts another collection of compatible types:
// Suppose you already have a pre-populated Rock array Array<Rock> existingRocks = new Array<>(); existingRocks.add(new Rock()); existingRocks.add(new Rock()); // Convert it to an Array<GameElement> and add to the 2D array Array<GameElement> gameElementRocks = new Array<>(existingRocks); gameElementsArray.add(gameElementRocks);
3. Add a Reusable Method (Optional but Clean)
To make this process cleaner, add a generic method to your GameWorld class that accepts any subclass of GameElement and handles the conversion automatically:
public <T extends GameElement> void addElementArray(Array<T> elementArray) { // Convert the subclass array to a GameElement array and add it gameElementsArray.add(new Array<>(elementArray)); }
You can then use this method like so:
GameWorld world = GameWorld.getInstance(); Array<Enemy> enemies = new Array<>(); enemies.add(new Enemy()); enemies.add(new Enemy()); world.addElementArray(enemies); Array<PlayerCharacter> player = new Array<>(); player.add(new PlayerCharacter()); world.addElementArray(player);
Why This Works
Polymorphism lets us treat any subclass of GameElement as a GameElement itself. Since libGDX's Array supports construction from compatible collections, we can safely convert subclass-specific arrays to the parent type and nest them in your 2D array.
内容的提问来源于stack exchange,提问作者Berk Erzin

