Flash calls Lua Script: 3

Source: Internet
Author: User

Here is a specific example: http://rc.qzone.qq.com/100641772

This time we will build a cumulative demo. Previously, we have learned about the communication between Lua and as3, but there are many more. For example, if we read the return value of the Lua method in as3, an array with the first element as a Boolean value is returned. But I don't know how to read the expected results from this array. If a friend knows this, I hope I can answer it.

In this case, a class is set up as an intermediary for data communication between Lua and as3,

 1   Package middle  2   {  3       Public   Class  Middledata  4   {  5           Public  Static   VaR Dataarray: array = New  Array ();  6           Public  Function middledata ()  7   {  8   }  9           10           Public Function add (_ n: Int ): Void 11   {  12   Dataarray. Push (_ n );  13   }  14   }  15 }

This class does not actually do anything, but has a static attribute as data storage.

In the Lua file, we have done three things. 1) Create an accumulative function. 2) Create the data intermediary class created in as3. 3) Call the add method of this class, put the result of the accumulate function of Lua into the static attribute of the Data mediation class.

 1   Function fact (Num)  2      If (Num <=1  ) Then  3         Return   1  4      Else   5         Return Num + fact (Num- 1  )  6   End  7   End  8   9 Local v = as3.Class . Middle. middledata. New  ()  10   11   Function exe_fact (Num)  12 -- As3.trace (fact (Num ))  13 As3.call (V, "  Add  "  , Fact (Num ))  14 End

The next thing is relatively easy. Create an as3 project, call the Lua method, and then call the data intermediary class to obtain the returned value of the Lua method.

1       Public   Class  Studylua01 extends Sprite  2   {  3           Public   VaR Intarray: vector. < Int > = New Vector. < Int > ();  4           Private  VaR  Txtinput: textinput;  5           Private   VaR  BTN: button;  6           Private   VaR  LBL: label;  7           Public  Function studylua01 ()  8   {  9  Initpage ();  10 Loadlua ( "  ../Lualib/lua1.lua  "  );  11   }  12           13           Private Function initpage (): Void  14   {  15 Txtinput =New  Textinput ();  16 Txtinput. Move ( 10 , 10  );  17   Addchild (txtinput );  18 BTN = New  Button ();  19   Addchild (BTN );  20 BTN. Move ( 180 , 10  );  21 LBL = New  Label ();  22 LBL. Move ( 380 , 10  );  23 LBL. Text = "  0  "  ;  24  Addchild (LBL );  25   }  26           27           Private Function loadlua (_ URL: string ): Void  28   {  29               VaR Urlloader: urlloader = New  Urlloader ();  30 Urlloader. dataformat =Urlloaderdataformat. text;  31               VaR URLRequest: URLRequest = New  URLRequest (_ URL );  32 Urlloader. addeventlistener (event. Complete, function (E: Event ): Void  {  33   Executelua (urlloader. data );  34   });  35  Urlloader. Load (URLRequest );  36   }  37           38           Private Function executelua (_ luast: string ): Void  39   {  40               VaR Lua: luaalchemy = New  Luaalchemy ();  41 Lua. setglobal ( " This  " , This  );  42   Lua. dostring (_ luast );  43               44 BTN. addeventlistener (mouseevent. Click, function (E: Event ): Void  45   {  46                   VaR AR: array = Lua. dostring ( "  Exe_fact ( " + Txtinput. Text + "  )  "  );  47                   VaR Result: Int = Middledata. dataarray. Pop ();  48 LBL. Text = Result. tostring ();  49   });  50   } 51 }

To show the effect:

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.