Objects comunication with parent in java

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.

Answers


When you create a GameUnit, you could pass the MyWorld as a constructor argument. The GameUnit constructor could then store the MyWorld object in a member variable. Then when it needed to do collsion detection, it could ask the MyWorld for a list of potential partners by calling a method on that object.

In GameUnit:

private MyWorld myWorld;

public GameUnit(int,int,double, MyWorld m) {
    myWorld = m;
}

And then in MyWorld, when you make a GameUnit:

new GameUnit(1, 2, 1.0, this);

Then in all the methods of GameUnit, you have the MyWorld object in myWorld available for use.


Sort of Add a World property of type MyWorld to GameUnit. Change GameUnit's constructor to accept World as an argument and set World to it.

Add GetX and GetY methods to MyWorld, that wrap up m_MyUnits[i].getX() and getY with the index in the array as an argument.

to give something like

For(int i = 0;i < World.maxUnits;i++) {
  int targetX = World. getX(i);
  int targetY = World. getY(i);
  //Do some thing like collition detection with the data.
}

Saying that I think I'd invert this and do something like

If (World.Collide(this))
{
  // Die messily.
}

Need Your Help

Can I remove a digital signature from a DLL?

winapi dll code-signing

My installer build "signs" a DLL using a Code Signing certificate during the build process.