Call function from render method one time (libGDX)

Crowni

In this test code:

 @Override
    public void render() {
        test();
    }

    private void test() {
    }

How can I call test(); one time (NOT loop) ?

m.antkowicz

One way is simple flag. Just create a boolean that will tell you if test() was called

    boolean isTestCalled = false;

    private void test()
    {
        System.out.println("test!");
    }

    @Override
    public void render(float delta) 
    {
        if( !isTestCalled )
        {
            isTestCalled = true;
            test();
        }

        ...

you can change boolean to int and increase it every time you will call test() to limit test call count. I don't very like flags - it generates spaghetti code.


If it would be C++ the second option would be using static variable inside test function and just tell it how many times it should be called - but hey this is Java :). Statics are only for classes but you still can simulate it:

    //even inside your class
    static class Counter
    {
        public static int count = 0;
    }

    private void test(int limit)
    {
        if( ++Counter.count <= limit ) //here you can add something like ...&& limit > 0 to have and option to infinite calls when calling test(-1) for example
        {   
            System.out.println("test!");
        }
    }

    @Override
    public void render(float delta) 
    {
        test(2);

        ...

And last but not least - in case you want to call test() exactly on a start of render - just move it to the show() function. But I guess it is not what you want to do when you are creating a question like this.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to call an Activity from Render Method in libGDX for Android

From Dev

React call component's method from nested function in render method

From Dev

How to call one method from another inside function

From Dev

How to call one function(method) from another in a class?

From Dev

React Native call a function in a render method

From Dev

React - call handler from var in render function

From Dev

react call a function from ReactDOM.render

From Dev

Rest api Method call morethan one time

From Dev

Call one method from another one

From Dev

is there any function to call a string from one method into another overriden method in java?

From Dev

How to call the override pause method dynamically from Libgdx?

From Dev

How to render component from a function outside the render method in React?

From Dev

how to call function inside reactjs render method properly?

From Dev

Call a method from one controller inside another

From Dev

Method call from one class to another not working?

From Dev

How to call a Method from one class to another

From Dev

How to call parameters from one method to another?

From Dev

Call method on render page content from parent window WPF

From Dev

Call base method from inherited function pointer

From Dev

Call top level function from groovy method

From Dev

Call base method from inherited function pointer

From Dev

Call a 'global' function from an object's method

From Dev

Call external function(from one exe to another)

From Dev

Call external function(from one exe to another)

From Dev

Call a python function from one class in another

From Dev

How to call a function in the render function?

From Dev

How to call a function in the render function?

From Dev

How to call one method from another method in javascript?

From Dev

Meteor.call() one method per time restriction, or async method call blocking another methods

Related Related

  1. 1

    How to call an Activity from Render Method in libGDX for Android

  2. 2

    React call component's method from nested function in render method

  3. 3

    How to call one method from another inside function

  4. 4

    How to call one function(method) from another in a class?

  5. 5

    React Native call a function in a render method

  6. 6

    React - call handler from var in render function

  7. 7

    react call a function from ReactDOM.render

  8. 8

    Rest api Method call morethan one time

  9. 9

    Call one method from another one

  10. 10

    is there any function to call a string from one method into another overriden method in java?

  11. 11

    How to call the override pause method dynamically from Libgdx?

  12. 12

    How to render component from a function outside the render method in React?

  13. 13

    how to call function inside reactjs render method properly?

  14. 14

    Call a method from one controller inside another

  15. 15

    Method call from one class to another not working?

  16. 16

    How to call a Method from one class to another

  17. 17

    How to call parameters from one method to another?

  18. 18

    Call method on render page content from parent window WPF

  19. 19

    Call base method from inherited function pointer

  20. 20

    Call top level function from groovy method

  21. 21

    Call base method from inherited function pointer

  22. 22

    Call a 'global' function from an object's method

  23. 23

    Call external function(from one exe to another)

  24. 24

    Call external function(from one exe to another)

  25. 25

    Call a python function from one class in another

  26. 26

    How to call a function in the render function?

  27. 27

    How to call a function in the render function?

  28. 28

    How to call one method from another method in javascript?

  29. 29

    Meteor.call() one method per time restriction, or async method call blocking another methods

HotTag

Archive