A practical guide to the development of LUA games Learning Note 2

Source: Internet
Author: User
Tags floor function square root

Learn lua in depth

First, the function

functions are the main tools for dividing game scripts.

The function starts with the function keyword followed by the name of the argument and then the list of arguments, ending with the end keyword

650) this.width=650; "title=" 1.jpg "src=" Http://s3.51cto.com/wyfs02/M01/54/80/wKioL1SFK8KCthhjAAA3UkNssQk519.jpg " alt= "Wkiol1sfk8kcthhjaaa3uknssqk519.jpg"/>

Example of a single parameter

function SetName (myString)

Print ("")

Print ("Your name is:", myString)

Print ("")

End

"MyString" is passed to the function and used in the function, and the arguments in the function are local variables, which are recycled after the call is finished .

Lua can define a variable-length parameter list, using the (... Instead of the parameter list, LUA creates a local table with the name ARG, saves all invocation arguments, and the number of arguments (obtained by ARG.N)

function Howmany (...)

...

End

return value

function uses the return keyword followed by a numeric value (usually a variable name) to return the result

function Timestwo (my)

my = my * 2

return my

End

Function can also return multiple results

function Threedice ()

D1 = Math.random (1,6)--Take random numbers

D2 = Math.random (1,6)

D3 = Math.random (1,6)

Mytotal = D1+d2+d3

Return d1,d2,d3,mytotal

End

You can also call another function using return

Standard library

assert (myvalue) ()

The Assert function can run a compiled block of code like a function (chunk)

Dofile (filename)

Dofile loads and immediately executes a LUA script file, typically used to load a defined file for invocation, or to load a data file (such as an archive)

Example: Dofile ("Scripts/function.lua")

mathematical Operators , LUA provides a function-level mathematical operator that can invoke the C standard library.

Math.Abs
Take absolute value
Math.max
The maximum value in a collection
Math.acos
Inverse cosine function
Math.min
The minimum value in a collection
Math.asin
Inverse chord function
Math.mod
Take the mold
Math.atan
Inverse tangent function Math.pow
Two x x, y
Math.atan2

Math.rad
Angle to radians
Math.ceil
Take a minimum integer value that is not less than the function parameter Math.sin
Zheng Xuan
Math.Cos
Yu Xuan
Math.sqrt
Calculate square root
Math.deg
Radian to Angle
Math.tan
Tangent
Math.exp
Calculates the exponential power of E. parameter is exponential Math.frexp
Exponential calculation, two parameters, first parameter is base, second parameter is exponent






Math.floor Rounding down
Math.ldexp Exponential calculation (x*2^y), two parameters, first parameter x is, second parameter is exponential
Math.log
Calculates the pair value of an e-based parameter X
Math.random
Random generation of pseudo-random numbers between 0~1
Math.log10
With Math.log
Math.randomseed
Set random number Seed






Common functions

Math.floor ()

The floor function is used to take down the whole (the concept of no floating-point or integer in Lua), which is simply to take out the fractional part

such as: a = 5.125

b = 5.72

Print (Math.floor (a))

Print (Math.floor (b))

The output is all

5

5

If you want to achieve rounding , then you can add 0.5 to a, b

Math.random ()

Math.random randomly generates pseudo-random numbers between 0~1, and Lua can pass in the maximum and minimum values, randomly generating numbers in this range.

such as: Mfdie = Math.random (1,6)

Math.min (), Math.max ()

Math.min () determines the maximum and minimum values in a collection

Update to here today first ...

A practical guide to the development of LUA games Learning Note 2

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.