Now, I have been looking for the answer to this for a while, but Google and all my power seems to fail me.
I got this Class. (MyWorld).
Within MyWorld I have an Array of objects
GameUnit m_MyUnits[] = new GameUnit[maxUnits];
Right now m_MyUnits are filled up with GameUnits and subclasses here off, and the are walking around the screen happy as cheesecake.
I have even written my own little collision detection for each of them, so they won’t bump into each other.
And all works fine, BUT!
For my collision detection to work I have chosen the slowest of ways (and most memory requiring I think)
In MyWorld I first loop trough each GameUnit and gets its X and Y position for an array
private int[][] Positions = new int[2][maxUnits]);
I then loop trough each and every GameUnit and assigns this array to it. (GameUnit has its own array called Positions for this)
Now, while this works, I can’t help but think that this is stupid like …. Well, I won’t use those words here.
Is there a way for GameUnit to Call the Positions in MyWorld, or even better, for the game unit to call the Array m_MyUnits[].getX() and m_MyUnits[].getY() in MyWorld
Some thing like this (I use Parent to refer to MyWorld from the GameUnit)
For(int i = 0;i < Parent.maxUnits;i++) {
int targetX = Parent. m_MyUnits[i].getX();
int targetY = Parent. m_MyUnits[i].getX();
//Do some thing like collition detection with the data.
}
Is this possible, or?
Yours JavaApprentis.
When you create a
GameUnit, you could pass theMyWorldas a constructor argument. TheGameUnitconstructor could then store theMyWorldobject in a member variable. Then when it needed to do collsion detection, it could ask theMyWorldfor a list of potential partners by calling a method on that object.In
GameUnit:And then in
MyWorld, when you make aGameUnit:Then in all the methods of
GameUnit, you have theMyWorldobject inmyWorldavailable for use.