Flash calls Lua Script: 4

Source: Internet
Author: User

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

Today, let's take a look at the iterator in Lua.

An iterator is a mechanism that allows you to traverse all elements in a set. In Lua, The iterator is usually represented as a function. Each call to the iterator function returns the next element in the set.

Each iterator must be in a certain state in each call so that it can know its location and how to proceed to the next step. Closure provides good support for such tasks. A closure is a local variable function that can access its external nested environment. For closure, these variables can remain in an intermediate state.

Let's write a simple iterator:

 
Function values (t) Local I=0ReturnFunction () I= I +1;ReturnT [I] endend

Next, create some data:

 Local days = { "   Sunday  " ,  "  Monday  " , "   Tuesday  " ,  "  Wednesday  " , "   Thursday  " ,  "  Friday  " , "   Saturday  "  } 

Write something to test it. Here I mention that writingProgramLike a sausage, write it at 1.1 o'clock and test it if you write it. Or you have an idea. You have to do this for a function. Do not run your program after writing a large segment, so if there is an error, it will have to pay a much higher price to track than to start testing. In the long run, I lost confidence in my programs step by step. Because of this, I like F #, Lua, and Python very much, so that I can see my thoughts and get feedback at any time.

 
DoForItemInValues (days)DoPrint (item) endend

We have obtained the correct results, which laid the foundation for the next step. So far, what we have done is in Lua. Next we will connect to flash.

In the previous three sections, we talked a lot about calling Lua scripts in flash.

UpperCode:

 Package {import flash. display. Sprite; import Fl. Controls. Button; import Fl. Controls. label; import flash. utils. bytearray; import luaalchemy. luaalchemy; import org. Middle; Public   Class  Iterator extends sprite {[embed (Source = "  ../Luasloud/iterator. Lua  " , Mimetype = "  Application/octet-stream  "  )]  Private   Static   VaR  Luaclass: class;  Public Function iterator (){  VaR T: middle = New  Middle (); initpage ();}  Private Function initpage (): Void  {  VaR Luaarray: bytearray = New Luaclass () As  Bytearray;  VaR Luast: String =Luaarray. readutfbytes (luaarray. bytesavailable );  VaR Luaalchemyobj: luaalchemy = New  Luaalchemy (); luaalchemyobj. setglobal (  "  This  " , This  ); Luaalchemyobj. dostring (luast); middle. setcanvas (  This  ); Luaalchemyobj. dostring (  "  Docircle () "  );}}} 

Create a class that executes the layout of the calendar page, and create a static method, with the date string as the parameter.

 Package org {import Fl. Controls. label; import flash. display. Sprite;  Public   Class  Middle {  Private   Static   VaR  Canvas: SPRITE;  Private   Static   VaR Space: Int = 0  ;  Public  Function middle (){}  Public   Static Function makcalendar (_ day: string ): Void  {  VaR Label: Label = New  Label (); label. Text = _ Day; canvas. addchild (Label); space + =100  ; Label. x = Space; label. Y = 20  ;}  Public   Static Function setcanvas (_ canvas: SPRITE ): Void  {Canvas = _ Canvas ;}}} 

The last step is to return the script called in flash, which is similar in that it only changes the output statement to the calendar construction statement.

As3.trace ( "  OK, Lua is ready. "  ) Local MD = As3. Class  . Org. middlelocal days = {  "  Sunday  " , "  Monday  " , "  Tuesday  " , "  Wednesday " , "  Thursday  " , "  Friday  " , "  Saturday  "  } Function values (t) Local I = 0    Return  Function () I = I + 1  ; Return  T [I] endfunction docircle ()  For Item In Values (days) Do  Md. makcalendar (item) endend 

We finally got a calendar in flash. The data in this calendar is created using the Lua script and read cyclically.

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.