Flash calls Lua Script: 2

Source: Internet
Author: User

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

In this section, we will further learn how to use luaalchemy.

As in the previous section, first create an as3 project.

 Public   Class  Loweveldemo1 extends sprite {  Public  Function loweveldemo1 () {initpage (); loadlua (  "  ../Luaassets/lowevel. Lua  "  );}  Private Function initpage ():Void  {  VaR  Label: label;  VaR BTN: button = New  Button (); BTN. Move (  100 , 100  ); Addchild (BTN );  VaR  C: myclass ;}  Private Function loadlua (_ URL: string ): Void {  VaR Urlloader: urlloader = New  Urlloader (); urlloader. dataformat = Urlloaderdataformat. text;  VaR URLRequest: URLRequest = New  URLRequest (_ URL); urlloader. addeventlistener (event. Complete, function (E: Event ):  Void  {Executelua (urlloader. Data) ;}); urlloader. Load (URLRequest );}  Private Function executelua (_ luast: string ): Void  {  VaR Lua: luaalchemy = New  Luaalchemy (); Lua. setglobal (  "  This  " , This  ); Lua. dostring (_ luast );}} 

This is a main file. In this file, we load a Lua script. Put a button on the page.

Let's take a look at the Lua script.

-- Luaalchemy -- Http: // Code.google.com/p/lua-alchemy/wiki/luatoas3lowlevel -- [[Learn more about luaalchemy and Lua Scripts] -- As3.trace (  "  Lua demo  "  ) -- As3. Class . Fl. Controls. Button. New  () -- BTN = as3. New ( "  Fl. Controls: button  " ) This statement always fails. Function luamethod () as3.trace (  "  OK  "  ) Endlocal lab1 = As3. Class . Fl. Controls. label. New  () Lab1.text = "  This is a Lua alchemy demo.  "  Lab1.width = 200  This . Addchild (lab1) lab1.move (  10 , 10  ) Local ST1 = As3. New ( "  String  " , "  Hello world!  "  ) Local V, n = As3.tolua (ST1, 40  ) As3.trace (type (V) as3.trace (type (n) as3.trace (type (ST1) Local m =As3.toas3 (luamethond) as3.trace (m) as3.trace (luamethond) lab1.text = St1local M1, M2 = As3.toas3 ( 45 , Function () Local m = 100  End) as3.trace (type (M1) as3.trace (type (m2) Local m3 = As3. New ( "  Array  "  ) M3 [  0 ] = "  A "  M3 [  1 ] = "  B  "  Local M4 = As3. Get (M3, "  Length  "  ) As3.trace (type (M4) as3.  Set (Lab1, "  Text  " ,"  Change by Lua  "  ) As3.trace (as3.type (m3) Local customclass = As3. Class . Custompack. myclass. New  () As3.trace (as3.type (customclass) as3.call (customclass. C1,  "  M1  "  ) Local C5 = As3. Get (Customclass, "  P1 "  ) Local C6 = As3.tolua (C5) as3.trace (C5) as3.trace (C6) 

The myclass class is displayed in the script.

 Package custompack {  Public   Class  Myclass {  Public   VaR P1: Int = 100  ;  Public   VaR C1: myclass2 =New  Myclass2 ();  Public  Function myclass (){}  Public Function hellomethod (_ n: Int ): Void  {Trace (p1 * _ N ));}}} 
  package custompack {  Public   class   myclass2 {  Public   var  P1:  int  =  33  ;  Public   function myclass2 () {}  Public  function M1 ():  void   {trace ( "   hello, m1   "  );}}}  

In addition, if you want to call the function of the Lua file in the as file, it is easy to add the following event for the button.CodeYou can.

 
, Btn2.addeventlistener (mouseevent. Click, function (E: Event ):Void{VaRResult: array = Lua. dostring ("Tempmethod ()");});

Now, we can not only access the methods in as from Lua, but also access Lua from.

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.