Search code examples
androidlibgdx

How to draw mathematical equation in libgdx?


I am making a math game in libgdx. I have Generate random problems and store in ArrayList. Now I want to draw these Mathmatical expressions on screen Scree, Actually SCreen have 3 part

                 ---------------------------------------------
                |     Score                    Sound          |
                |                                             |
                 ----------------------------------------------
                |   3-1=2                                     | 
                |                                             |
                |                           8+9=4             |
                |                                             |
                |                                             |
                |                                             |
                |                                             |
                |              5*4=20                         |
                |                                             |
                |                                             |
                 ----------------------------------------------
                |  Virtual keyboard of                        |
                |  numbers (0-9)for user input                |
                 ----------------------------------------------

Anyone guide me how I can draw like this these expression are moving top to bottom.


Solution

  • You could use the power of Action class. Just create Label's and attach MoveTo (or MoveBy) action to them. Please read more info about actions here.

    Stage yourStage = ...
    Group root = yourStage.getRoot();
    
    // create some label
    final Label mathExpression = new Label("3-1=2", yourSkin, "labelSkinName");
    // set initial position, could be random
    mathExpression.setPosition(randomXPosition, topPosition); 
    
    // create sequence action, where actions are run one by one
    SequenceAction sequence = new SequenceAction(); 
    // add simple action that moves actor down by topPosition with 4sec to the sequenca
    sequence.addAction(Actions.moveBy(0, -topPosition, 4)); 
    // add Runnable action that will be run after that to the sequence
    sequence.addAction(Actions.run(new Runnable()
    {
        @Override
        void run()
        {
            // this code will be run after label fall, for example, remove it
            mathExpression.remove();
        }
    }
    
    // attach your actions sequence to the label
    mathExpression.addAction(sequence);
    
    // for example, attach label to root
    root.addActor(mathExpression); 
    

    You could create those labels one by one and actions will take care of them.

    I hope it helps you.