I have a class called LinkGroup which holds some game objects. I call Rotate to set some rotation variables for these objects. Whenever my game hits its update loop, I rotate the objects according to the rotation variables. If they’ve rotated enough, I fire an onComplete callback.
The following code works…
public void Rotate(){
_currentRotation = _0;
_targetRotation = 180; //degrees
_rotationSpeed = 50;
try{
_onComplete = LinkGroup.class.getDeclaredMethod("rotateComplete", null);
}
catch(Exception ex){
}
}
…but this is ugly.
I don’t like having to declare the method rotateComplete and manually link it to Rotate via a string. Is there something similar to anonymous functions in C# so I can just declared the rotateComplete method inside the Rotate method?
For bonus points, is there a better way to implement the required exception handling for “getDeclaredMethod”? Terseness is a preference.
From my understanding, I believe you are trying to call
onRotateComplete()method in theLinkGroupclass whenever some game object is been rotated. You can use the pattern that Java Swing uses for handling button clicks or other events: This could be done this way:Define an interface
Change the
Rotate()toRotate(IRotateHandler handler)and then inLinkGroupclass you can call your game object like this.