So I’m finally starting learn efficient programming (Used to use one class for everything), and this new game I’m making has the following classes so far:
- GameDriver
- Map
- Block
- Player
And as of right now, Map creates an ArrayList of Block objects, and the GameDriver creates a Map object and a Player object, now that’s all working perfectly fine and good. But now I ran into a problem.
I added a method to Player called checkLand() and GameDriver calls that method to check if the player can step on a certain spot, but for the method to be able to check it, it needs access to the map object that the GameDriver is using so I tried this:
public class Player(){
GameDriver gameDriver = new GameDriver();
...more declarations
public boolean checkLand(int xCord, int yCord){
if (gameDriver.map.world.get(gameDriver.map.getByCords(xCord,yCord)).type == 1) {
return false;
} else {
return true;
}
}
But obviously it doesn’t work, but I wanted to show you guys so you could get an understanding of what I want it to do, should I just have the checkLand method in GameDriver? I want to know the best way please 🙂
Since the
Playerobject is being created by theGameDriverobject, you should be able to access thePlayerobject within yourGameDriverclass. Also, you said that theMapobject is also being created within yourGameDriverclass, so that is also available.What you could do would be to have a method in your
Playerclass which takes aMapobject as parameter and does the logic you need.This will allow your
GameDriverobject to call an object which it has (Player) and pass it another object it also has (Map).