简体   繁体   中英

using constructor from another class in java to create objects in separate classs

how can I use the constructor from another class in java to make an object through a method in separate class. For example below is a constructor in a player class

public class Player extends Entity {



public Player(int maxEnergy, int x, int y) {
        this.maxEnergy = maxEnergy;
        this.energy = maxEnergy;
        carryingGhost = false;
        xPos = x;
        yPos = y;
    }

Which I want to use and create objects (player) through a method called

private Player createPlayer() { 

and the above method is in separate class as

public class GameEngine {


**The method must return a Player object that represents the player in the 
game.  it must set the maxEnergy for the player, and the
X and Y positions corresponding to a tile position in the current level.    

I have tried to initialize player within method with parameters and without parameters as**

Player player = new Player(int maxEnergy, int x, int y);

    this.player.getEnergy();
    this.player.getMaxEnergy();
    this.player.setPosition(x, y);
    
       return player;       
    }

But it give errors.Any help will be appreciated.I am quite close to assume its not possible to have created objects like this.

below I share the complete game engine which is working with other classes as well.

import java.awt.Point;
import java.util.ArrayList;
import java.util.Random;


public enum TileType {
        WALL, FLOOR1, FLOOR2, BANK, BREACH, DOOR;
    }


public static final int LEVEL_WIDTH = 35;

public static final int LEVEL_HEIGHT = 18;

private Random rng = new Random();

private int levelNumber = 1;  //current level

   
private int turnNumber = 1;

private GameGUI gui;

private TileType[][] level;

private ArrayList<Point> spawnLocations;

   
private Player player;

   
private Ghost[] ghosts;

   
public GameEngine(GameGUI gui) {
    this.gui = gui;
}
private TileType[][] generateLevel() {
    //YOUR CODE HERE
  
    return null;        //change this to return the 2D arrayof TileType 
                        //values that you create above
}
private ArrayList<Point> getSpawns() {
    ArrayList<Point> s = new ArrayList<Point>();
    // YOUR CODE HERE
    return s;
}
   
private Ghost[] addGhosts() {
    //YOUR CODE HERE
    
    return null;       //change this to return an array of ghost objects 
}

**/**
 * Creates a Player object in the game. The method instantiates
 * the Player class and assigns values for the energy and position.
 * The first version of this method should use fixed a fixed position 
 for  the player to start, by setting fixed X and Y values when calling 
 the constructor in the Player class. The second version of this method 
 should use the spawns ArrayLis to select a suitable location to spawn 
 the player and removes the Point from the spawns ArrayList. This will 
 prevent the Player from being added to the game inside a wall, bank or 
 breach for example. 
 
  @return A Player object representing the player in the game
 */**

private Player createPlayer() {
    //YOUR CODE HERE
    
    return null;        //change this to return a Player object
}
    
    public void movePlayerLeft() {
        
    }

    public void movePlayerRight() {
        
    }

    
    public void movePlayerUp() {
        
    }

   
    public void movePlayerDown() {
        
    }

  
    private void hitGhost(Ghost g) {
        
    }
    private void moveGhosts() {
        
    }

   
    private void moveGhost(Ghost g) {
        
    }

    
    private void cleanDefeatedGhosts() {
        
    }

   
    private void nextLevel() {
        
    }

   
    private void placePlayer() {
        
    }

    public void doTurn() {
        cleanDefeatedGhosts();
        moveGhosts();
        gui.updateDisplay(level, player, ghosts);
    }

    public void startGame() {
        level = generateLevel();
        spawnLocations = getSpawns();
        ghosts = addGhosts();
        player = createPlayer();
        gui.updateDisplay(level, player, ghosts);
    }
}

Does something like this work for your case?

public class GameEngine {
  private Player createPlayer() {
    return new Player(1,2,3);
  }
}

Add a default no-args constructor in the player class. Once you create a constructor with Arg, java will not auto provide default one.

You have already declared Player

private Player player;

So you must not try to reinitialize using same variable name, rather

private Player createPlayer() {
  Player newPlayer = new Player();
  // set the different props of the Player obj
return newPlayer ;        

}

What is the error which you are facing? Can you share that?

The following should do it:

private Player createPlayer() { 
   int defaultMaxEnergy = 10;  // Whatever value it should have
   int initialX = 1;  // Whatever value it should have
   int initialY = 1;  // Whatever value it should have
   
   return new Player(defaultMaxEnergy, initialX, initialY);  
}

Since the values are not in your descriptions I just selected a random number but you can pick whatever integers you want and that makes sense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM