Developing a game with AndEngine GLES2
I am having an issue trying to change the scale of a rectangle that I have added to a scene from within a TimerHandlers callback. Whenever the timer ticks it makes a percentage calculation and then within the OnTimePassed ITimerCallback it tries to set the scale of the rectangle in the scene. Unless I declare the rectangle as static I keep getting a nullreferenceexception error.
I know that this is a scope issue, my question is, should I be declaring the objects I need to access from within the timercallback as static or is there an “accepted way” to get access to Activity level objects from within a timercallback?
PlayerRecastTimer = new TimerHandler(0.5f, true, new ITimerCallback(){
private float recastTime = playerSpellToCast.getRecastTime();
public void onTimePassed(TimerHandler pTimerHandler) {
if(recastTime == 0) {
mAct.GetEngine().unregisterUpdateHandler(pTimerHandler);
mAct.GetQueue().RemovePlayerSpell();
pRecast.setScaleX(1);
RunScene();
}
float recastScale;
recastScale = recastTime / playerSpellToCast.getRecastTime();
pRecast.setScaleX(recastScale); //blows up here when attempting to scale the rectangle
recastTime -= 0.5;
}
});
If your
PlayerRecastTimeris a member of a class that containspRecast, then you can reference it usingClassName.this.Example: If you have a class named
LoadingScreenActivityand it has a rectangle calledpRecast, then you can accesspRecastin youronTimePassed()function by using