2

I have ui objects that allow me to overwrite their tick methods and I want to call other methods in these tick methods. But the way I'm doing it, the objects haven't been initialised yet so it's not working. Here's an example:

    //FPS       
    UITextRectangle fps = new UITextRectangle(
        handler, handler.getWidth()-64, 32, 32, 32, Color.DARK_GRAY, Color.GRAY,
        new Ticker(){
          @Override
          public void doTick(){
            //cannot call fps
            fps.setText(Integer.toString(handler.getFPS()));
          }
        });

How can I make this work? Any and all help and advice is appreciated!

Edited because my code was wrong.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
Flash
  • 37
  • 8
  • what do you mean by "it's not working"? Do you get a compiler error? Exception? – JohnnyAW May 07 '16 at 10:32
  • I suggest to delete or close this questions, because it seems to be a typo problem... – Marcinek May 07 '16 at 10:33
  • "The local variable fps may not have been initialized" Because it's trying to call itself before it's finished initializing, sorry if you saw the code before I edited it. I messed it up the first time. – Flash May 07 '16 at 10:34
  • did answer with `final` worked? – JohnnyAW May 07 '16 at 10:38
  • Apologies for the sheer vanity of marking this a duplicate of a question that I asked and answered myself, but I think that this should give you a good idea as to why this isn't possible. Wouldn't `this.setText(...)` (or simply `setText(...)`) work? – Andy Turner May 07 '16 at 10:49
  • @Flash `How can I call object methods in the constructor of non initialized objects?` Why can't you initialize the objects first? What is prevent you from doing that? – user3437460 May 07 '16 at 10:50
  • @AndyTurner It's an interesting answer, but I wouldn't say it's a good duplicate, as it doesn't give a possible solution to the specific problem here. This questions is not asking _why_ it occurs, it is asking for a workaround. – Jorn Vernee May 07 '16 at 10:56
  • @JornVernee well, ok - but without information as to the available methods of the `UITextRectangle` (e.g. is there a `setTicker()` method?) it's hard to recommend the "correct" solution. – Andy Turner May 07 '16 at 11:01
  • The Ticker interface has a `doTick()` method, the `UITextRectangle` has a `setText(String)` method. I could create a `setTicker()` method in `UITextRectangle` if that seems to be the best solution? `setText(...)` does not work because it is a part of the `UITextRectangle` class, not the `Ticker` – Flash May 07 '16 at 11:19

3 Answers3

1

Instead of passing a the Ticker class, you could have a doTick template method:

class UITextRectangle {

    public void doTick() {
        // template method, is empty
    }
}
...
UITextRectangle tr = new UITextRectangle(...) {

    @Override
    public void doTick() {
        this.setText(...);
    }
};
Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93
  • This worked, thank you!! I was starting to get lost with the technicalities of the other answers. Thanks so much – Flash May 07 '16 at 11:25
0

try calling it directly, you dont need to use the fps object because you are in the callback of the object self

try this:

UITextRectangle fps = new UITextRectangle(handler, handler.getWidth()-64, 32, 32, 32, Color.DARK_GRAY, Color.GRAY, new Ticker(){
        @Override
        public void doTick(){ 
            setText(Integer.toString(handler.getFPS()));
        }
    });
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

create a property UITextRectangle fps in your class.

Another solution would be to set the Ticker after the initialization

JohnnyAW
  • 2,866
  • 1
  • 16
  • 27