Functions.lua Directory of Quick Lua 3.3 common methods and learning techniques

Source: Internet
Author: User

1.functions.lua (Framework->functions.lua)

Provides a common set of functions, as well as extensions to the Lua standard library

1.printf

2.checknumber checkint checkbool checktable isset3. Deep Clone A value clone

4. Create an Classes class

5. Loading a module import () is the same as the Require () function, but with some degree of automation

6. Wrapping Lua objects and their methods as an anonymous function
In Quick-cocos2d-x, many functions need to pass in a Lua function to do parameters,
The incoming function is then called when a particular event occurs. such as touch events, frame events, and so on.
~ ~ ~ LUA

Local MyScene = Class ("MyScene", function ()
Return Display.newscene ("MyScene")
End

function Myscene:ctor ()
Self.frametimecount = 0
--Register Frame Event
Self:addnodeeventlistener (CC. Node_enter_frame_event, Self.onenterframe)
Self:scheduleupdate ()
End

function myscene:onenterframe (DT)
Self.frametimecount = self.frametimecount + dt
End

~~~

The above code executes with an error, reporting "Invalid self", because C + + does not recognize Lua object methods. So we did not provide the correct arguments when we called the Self.onenterframe method we passed in.

To make the above code work properly, you need to use handler () to do the packing:

~ ~ ~ LUA

function Myscene:ctor ()
Self.frametimecount = 0
--Register Frame Event
Self:addnodeeventlistener (CC. Enter_frame_event, Handler (self, self.onenterframe))
Self:scheduleupdate ()
End

~~~

In fact, in addition to the C + + callback Lua function, handler () can be used in all other places where callbacks are needed.

Some methods of 7.math
(1) Math.newrandomseed () initializes the random number seed according to the system time, allowing subsequent math.random () to return a more random value
(2) Math.Round (value) rounds the value and returns 0 if it is not a value
(3) Math.angle2radian (angle) angle to radians
(4) Math.radian2angle (radian) radian Turn angle

8.io

  (1) io.exists (path) file is not present note: Call Io.open (path, "R") to determine
  (2) Io.readfile ( Path) Read file contents Io.open (path, "R") File:read ("*a")
  (3) io.writefile (path, content, mode)
      Writes the file as a string, returns True successfully, returns false
      The mode write mode parameter determines how the content is written by Io.writefile (), the following values are available:
    & nbsp -"w+": Overwrite the contents of the file, create a new file if the file does not exist
     -"A +": Append content to the end of the file, create a file if the file does not exist
      In addition, you can also write The "in mode" parameter is appended with the character "B", which means that the data is written in binary mode, which avoids the incomplete writing of the content.
      **ANDROID Special NOTE: * * on the Android platform, files can only be written to the path of the memory card, assets and data and other directories are not writable.

(4) Io.pathinfo (path) splits a path string, returning parts of the constituent path
Example:
Local pathinfo = Io.pathinfo ("/var/app/test/abc.png")
--Results:
--Pathinfo.dirname = "/var/app/test/"
--Pathinfo.filename = "Abc.png"
--Pathinfo.basename = "ABC"
--Pathinfo.extname = ". png"
(5) io.filesize (path) returns the size of the specified file if the failure returns false


9.table

  (1) table.nums (t) the "#" Operation of Lua table is only valid for sequentially ordered numeric subscript arrays, and table.nums () evaluates all non-nil in table The number of values.
  (2) Table.keys (hashtable) returns all keys
  (3) table.values (hashtable) returns all values
  (4) Table.merge ( Dest, SRC) copies all the keys and their values from the source table to the target table object, overriding its value if there is a key with the same name
  (5) Table.indexof (array, value, begin) finds the specified value from the table, returns its index, If not found returns false
  (6) table.keyof (Hashtable, value) finds the specified value from the table, returns its key, if not found, returns nil
  (7) Table.removebyvalue (array, value, RemoveAll) removes the specified value from the table, returning the number of deleted values
  (8) table.map (T, FN) executes the specified function once for each value in the table, Update table contents with function return value the
      fn parameter specifies a function that has two parameters and returns a value. The prototype is as follows:
      function map_function (value, key)
      return value
    &NB Sp End
(9) table.walk (T, FN) executes the specified function once for each value in the table, but does not change the table contents
    The FN parameter specifies a function that has two parameters and no return value. The prototype is as follows:
    function map_function (value, key)
    End
(Ten) Table.filter (T, fn)


(one) Table.unique (T, Barray) traverse the table to ensure that the values are unique

String.htmlspecialchars (input) converts special characters to HTML escape character

String.restorehtmlspecialchars (input) restores the HTML escape character to a special character, and the function is the opposite of String.htmlspecialchars ()

(+) STRING.NL2BR (input)

(string.split) (input, delimiter)
--Splits the input string with the specified character or string, returning an array containing the result of the split
--@function [parent= #string] Split
--@param string input strings
--@param string delimiter to split the marker character or string
--@return Array#array contains an array of split results


String.ltrim (input) removes white space characters from the header of the input string and returns the result

String.formatnumberthousands (NUM) formats a numeric value as a string regular expression that contains a thousand-bit delimiter

Print (String.formatnumberthousands (1924235))
--Output 1,924,235

Functions.lua Directory of Quick Lua 3.3 common methods and learning techniques

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.