lua热跟新学习

版权声明:转载 请说明 https://blog.csdn.net/qq2512667/article/details/83502433

入门

Lua table中的下标 从1开始,没有++ -- 运算符

逻辑 运算符  and  not 分别表示 与或非  相当于&& ||

注释 用 两个- 来表示 ,   -- num=1

声明变量不用 声明 类型。 nil 相当于其他语言的null类型

一行代表一个语句 不需要 分号结束

if 语法

if a>0  then

print(a)

end

if  a>0 then

elseif a>-1 then

else  

end

while 语法 

while  index<100  do

index=index+1  

end

repeat 语法

repeat

until  index==100

for语法

for index=1,100 do

     index=index+1

end 

再lua中  for 只有 break 没有 continue。

字符处理

 ..  表示 连接字符串 

table的 遍历

names{ jack="jack",Math="20",age="18"}

for index,value in pairs(names)  do

      print(index,value)

end

Lua 中 table 的面向对象

例子 

Enemy ={}

 local this=Enemy

Enemy.hp=100

Enemy.mp=50

Enemy.MoveTo= function(a)

 print("移动到",a)

End

function Enemy.Attack(atk)

 print("当前血量",this.hp)

   this.hp=this.hp-atk

 print("被攻击后血量",this.hp)

End

this. Attack(13)

Enemy.MoveTo(10)

猜你喜欢

转载自blog.csdn.net/qq2512667/article/details/83502433