LIBGDX's packaged simple scripting mechanism

Source: Internet
Author: User
Tags libgdx

Demand:

In the game, often used before the beginning, Ready->go->start game, or other situations.

Simply put, it's the game. Some UI displays need to follow the steps.

Thus, in order to make the logic clear, I wrote a simple script to facilitate the future development.

Package com.oahcfly.chgame.core.script;//Interface Public interface Iscript {public void Addsteps (String ... args);p ublic void Run (final Runnable callbackrunnable);}
Package com.oahcfly.chgame.core.script;import java.lang.reflect.invocationtargetexception;import  java.lang.reflect.Method;import com.badlogic.gdx.Gdx;import  com.badlogic.gdx.scenes.scene2d.actions.actions;import  com.badlogic.gdx.scenes.scene2d.actions.runnableaction;import  com.badlogic.gdx.scenes.scene2d.actions.sequenceaction;import com.badlogic.gdx.utils.array;import  com.oahcfly.chgame.core.mvc.chactor;import com.oahcfly.chgame.core.mvc.chscreen;/** *  *  ch Script:  * 1.add (method Name)  * 2.run (callback function)  *  *  @author  haocao *  */public class CHScript implements IScript {private Array<String>  stepArr = new Array<String> ();p rivate chscreen parentscreen;private  Chactor actor;public chscript (Chscreen screen)  {this.parentscreen = screen;actor  = new chactor ();} @OverRidepublic void run (final runnable callbackrunnable)  {//  calls in screen, a series of method names. Method1,mthod2,method3...this.parentscreen.addactor (actor); Sequenceaction sequenceaction = new sequenceaction ();for  (int i = 0,  size = steparr.size; i < size; i++)  {final String  Methodname = steparr.get (i); Runnableaction runnableaction = new runnableaction ()  {@Overridepublic  void  Run ()  {invokemethod (methodName);}; Sequenceaction.addaction (runnableaction);} Sequenceaction.addaction (Actions.run (new runnable ()  {@Overridepublic  void run ()  { Parentscreen.removeactor (actor); CHScript.this.stepArr.clear (); Callbackrunnable.run ();}); Actor.addaction (sequenceaction);} Private void invokemethod (String methodname)  {final class<?> screenclass  = parentscreen.getclass (); Method method;tRy {method = screenclass.getdeclaredmethod (MethodName); Method.invoke (Parentscreen);}  catch  (nosuchmethodexception e)  {// TODO Auto-generated catch  BlockGdx.app.error (GetClass (). Getsimplename (),  e.getlocalizedmessage ());}  catch  (securityexception e)  {// TODO Auto-generated catch  BlockGdx.app.error (GetClass (). Getsimplename (),  e.getlocalizedmessage ());}  catch  (illegalaccessexception e)  {// TODO Auto-generated catch  BlockGdx.app.error (GetClass (). Getsimplename (),  e.getlocalizedmessage ());}  catch  (illegalargumentexception e)  {// TODO Auto-generated catch  BlockGdx.app.error (GetClass (). Getsimplename (),  e.getlocalizedmessage ());}  catch  (invocationtargetexception e)  {// TODO Auto-generated catch  BlockGdx.app.error (GetClass (). Getsimplename (),  e.getlocalizedmessAge ());}} The/** *  method requires public.  */@Overridepublic  void addsteps (String... args)  {for  (string methodname  : args)  {steparr.add (MethodName);}}


The function core is to rely on the sequenceaction in Libgdx to execute the action in sequence. Finally, an end callback is given.

Introduction to Usage:

Chscript script = new Chscript (this), Script.addsteps ("Step1", "Step2", "Step3"), Script.run (new Runnable () {@ overridepublic void Run () {//TODO auto-generated method stub//System.out.print ("Step execution Completed");});


In screen, add the above code, and 3 methods are required:

public void Step1 () {System.out.println (System.currenttimemillis () + "Step1");} public void Step2 () {System.out.println (System.currenttimemillis () + "Step2");} public void Step3 () {System.out.println (System.currenttimemillis () + "Step3");}

This will enable us to achieve our needs.


Welcome to the CHGAME Framework (rapid development framework based on the Libgdx two package):

Https://git.oschina.net/oahcfly/CHGame.git

LIBGDX's packaged simple scripting mechanism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.