×

Langue

Fermer
Atelier 801
  • Forums
  • Dev Tracker
  • Connexion
    • English Français
      Português do Brasil Español
      Türkçe Polski
      Magyar Română
      العربية Skandinavisk
      Nederlands Deutsch
      Bahasa Indonesia Русский
      中文 Filipino
      Lietuvių kalba 日本語
      Suomi עברית
      Italiano Česky
      Hrvatski Slovensky
      Български Latviešu
      Estonian
  • Langue
  • Forums
  • /
  • Transformice
  • /
  • Modules
  • /
  • 【Lua教學】基礎Lua
【Lua教學】基礎Lua
Zxcew
« Citoyen »
1500033120000
    • Zxcew#0000
    • Profil
    • Derniers messages
#1
  2

Lua 是遊戲本身允許玩家在部落之家中,執行遊戲以外的程式。
使用方法是在部落之家中輸入/lua,並輸入想執行的程式

注意,以下原因可能導致Lua無法使用
  • 奶酪未滿 1000
  • 沒有 /np 權限
  • 超過 64000 個字元

首先你必須知道,遊戲提供的函式(function)以及事件(event),請參閱以下網址
http://atelier801.com/topic?f=5&t=451587#m3
一、指令

在一些小遊戲中,常常會有指令的存在(例: !help)
其實,做法非常簡單,在上述遊戲提供的事件中,
可以找到 eventChatCommand ( playerName, command )
其中有兩個參數,playerName 以及 command,分別指的是

  • playerName:輸入指令的玩家名
  • command:玩家輸入的指令


那我們要如何使用事件呢?
只要在事件名稱的前方加上 function ,換行後加上 end
其實不只這種做法,這邊提供我個人比較常用的。

給個例子,當玩家輸入 !mort 即死亡
Code Lua

1
2
3
4
5
function eventChatCommand ( playerName, command )
if command == "mort" then
tfm.exec.killPlayer(playerName)
end
end

讓我們一句一句來解析,第一句如果剛剛有認真看,應該是沒問題
不過這裡有一點要提醒,同一個lua裡面,一個事件只能出現一次,
接下來如果有接觸過程式的人,應該都很熟,
if 是用來判斷條件是否滿足的,若滿足則執行以下程式,一個 if 也必須搭配一個 end

這邊提供用來判斷的依據
  • ==:等於
  • >=:大於等於
  • <=:小於等於
  • ~=:不等於

要注意的是,程式中的 == (equal) 不可用 = (assign) 代替

所以 if command == "mort" then 的意思是,當輸入的指令等於 mort 時,
執行 tfm.exec.killPlayer(playerName) ,即殺死輸入該指令玩家,

值得注意的點是,這邊的每個參數都是 字串(String) ,所以當 command == "mort" 時的 mort 才須用 " " 框住,
代表 mort 須是一個字串,以下提供常用數據的類型,

  • 整數(Integer)
  • 字串(String)
  • 布林值(Boolean):只可為 True 或 False
  • 陣列(Table)

這些就是會遇到的數據類型,

若整數類型的數據需判斷時,只需在 == 後加上整數值即可,不須 " ",
(例: if score == 0 then)
字串類型則須加上 " ",
布林值只可為 是(True) 或 否(False)

指令的部分大概就到這邊,如果喜歡我的教學,歡迎右下角點個喜歡,
如果還有問題,也歡迎在下方提問。
如果有講錯的地方 歡迎指正

FAQ

Q : 每次打指令都會顯示在聊天欄上,真的很煩,有沒有方法可以消除呢?
A : system.disableChatCommandDisplay("這邊輸入你的指令", true) 把這行加入你的程式就可以了喔


Dernière modification le 1514784480000
Zxcew
« Citoyen »
1500194820000
    • Zxcew#0000
    • Profil
    • Derniers messages
#2
  2

二、空白鍵飛

相信大家都用過 #unility 房中的 !fly ,應該都很好奇怎麼做到案空白鍵來飛吧。
這就要用到
system.bindKeyboard ( playerName, keyCode, down, activate ) 、 eventKeyboard ( playerName, keyCode, down, xPlayerPosition, yPlayerPosition ) 和 tfm.exec.movePlayer ( playerName, xPosition, yPosition, positionOffset, xSpeed, ySpeed, speedOffset )

這邊一樣逐個解釋.
  • system.bindKeyboard:用來偵測玩家是否按下某按鍵
    • playerName (String):被綁定偵測的玩家名
    • keyCode (Int):某按鍵的代碼
    • down (Boolean): (是) 為在按下時偵測, (否) 為放開時偵測
    • activate (Boolean):是否啟用,一般都用 (是)



  • eventKeyboard:偵測玩家是否按下某按鍵的事件,但該玩家須被上述函式綁定
    • playerName (String):被偵測的玩家名
    • keyCode (Int):某按鍵的代碼
    • down (Boolean):玩家被偵測的是按下或放開
    • xPlayerPosition (Int):玩家按下按鍵時在地圖的 X 座標
    • yPlayerPosition (Int):玩家按下按鍵時在地圖的 Y 座標


  • tfm.exec.movePlayer:移動玩家的位置
    • playerName (String):欲移動的玩家名
    • xPosition (Int):目的地 X 座標
    • yPosition (Int):目的地 Y 座標
    • positionOffset (Boolean): (是) 則以現有的X,Y座標加上以上輸入的座標, (否) 則移動到以上輸入的絕對位置
    • xSpeed (Int):增加或減少玩家的水平速度
    • ySpeed (Int):增加或減少玩家的垂直速度
    • speedOffset (Boolean): (是) 則以現有的速度加上以上輸入的速度, (否) 則用以上輸入的速度覆蓋原本的速度



a dit :
system.bindKeyboard ("Zxcew", 32, true, true)
function eventKeyboard ( playerName, keyCode, down, x, y)
if keyCode == 32 then
tfm.exec.movePlayer(playerName, 0, 0, true, 0, -50 , true)
end
end

  • system.bindKeyboard ("Zxcew", 32, true, true):這邊玩家名字我使用我自己,當然也可以改成其他名字。
  • if keyCode == 32 then:偵測按鍵代碼若等於 32(SpaceBar) 就執行以下程式
  • tfm.exec.movePlayer(playerName, 0, 0, true, 0, -50 , true):移動按下按鍵的人,第二、三項參數設為 0 以及第四項參數設為 True ,
    則老鼠會留在原地,第六項參數設為 -50,老鼠就會往上,達成飛行的動作,當然飛多高也可以自由調整,甚至可以往下都可以。


那今天的教學就到這邊..
如果還有問題,歡迎在下方提問。
有講錯的地方 歡迎指正

FAQ
Q : 如果我想要一次把全部的人都綁定按鍵呢?
A :
a dit :
for name,player in pairs(tfm.get.room.playerList) do
system.bindKeyboard(name, 32, true, true)
end

把這段加入你的程式就可以了喔,這裡面的 name 代表所有玩家,原理的話以後會多做解釋


Dernière modification le 1514784660000
Kuaidi
« Citoyen »
1502347080000
    • Kuaidi#0000
    • Profil
    • Derniers messages
    • Tribu
#3
  2
教程写的很棒!代码缩进一下可能更好 ^ ^
Code Lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function eventChatCommand (playerName, command)
if command == "mort" then -- inline comment test1
tfm.exec.killPlayer( playerName )
end
end

system.bindKeyboard( "Zxcew", 32, true, true )
function eventKeyboard (playerName, keyCode, down, x, y)
if keyCode == 32 then
tfm.exec.movePlayer( playerName, 0, 0, true, 0, -50 , true )
end
end
--[[ block comment test2
test2 ]]
Zxcew
« Citoyen »
1514784780000
    • Zxcew#0000
    • Profil
    • Derniers messages
#4
  1
Kuaidi a dit :
教程写的很棒!代码缩进一下可能更好 ^ ^
Code Lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function eventChatCommand (playerName, command)
if command == "mort" then -- inline comment test1
tfm.exec.killPlayer( playerName )
end
end

system.bindKeyboard( "Zxcew", 32, true, true )
function eventKeyboard (playerName, keyCode, down, x, y)
if keyCode == 32 then
tfm.exec.movePlayer( playerName, 0, 0, true, 0, -50 , true )
end
end
--[[ block comment test2
test2 ]]

謝謝,用code可以縮排可是不能換顏色,quote可以改顏色卻不能縮排,為了讓大家方便了解,所以選了可以換顏色的。

Dernière modification le 1514784840000
Zxcew
« Citoyen »
1514789580000
    • Zxcew#0000
    • Profil
    • Derniers messages
#5
  1

三、抽獎(外型)

要做出抽獎機,首先必須準備
  • 一個按鈕
  • 一個顯示的介面
  • 抽獎的程式

抽獎機的原理是:

點擊按鈕
↓
經過抽獎程式
↓
顯示抽到的東西



所以,今天就要先把按鈕以及介面做出來。
在這之前,要先來介紹一個涵式。


ui.addTextArea ( id, text, targetPlayer, x, y, width, height, backgroundColor, borderColor, backgroundAlpha, fixedPos )
 用於產生文字區塊。
  • id(Int):這個文字區塊的ID,若重複,比較晚寫的會覆蓋之前的。
  • text(String):這個文字方塊裡要填入甚麼文字。
  • targetPlayer(String):顯示給目標玩家,若填入nil則將顯示給全部玩家。
  • x(Int):文字方塊左上角的 x 座標。
  • y(Int):文字方塊左上角的 y 座標。
  • width(Int):文字方塊的寬度(橫向)。
  • height(Int):文字方塊的高度(縱向)。
  • backgroundColor(Int):背景顏色,參照色碼表。(去掉 # 之後前面加上 0x)
  • borderColor(Int):邊框顏色,顏色調整方法同上。
  • backgroundAlpha(Number):背景透明度, 0~1 ,0為完全透明,1為完全不透明。可用小數。
  • fixedPos(Boolean):是否固定位置於螢幕上。(若否,則使用長圖時,文字方塊不會跟著移動。)

首先,先做出一個介面用於顯示抽中的獎品。
Code Lua

1
ui.addTextArea(0, "", nil, 100, 27, 600, 360)

接著,做出一個用於抽獎的按鈕。

Code Lua

1
ui.addTextArea(1, "<a href='event:draw'><font size='25'><p align='center'>點我抽獎</p></font></a>", nil, 120, 327, 560, 50)
這個方塊我做的時候Lua不能用,位置和大小我是純數學算的,如果跑掉請見諒。

Code Html

1
<a href='event:自訂事件名稱'></a>
使用這個就可以讓文字變成可點擊的,並且會回傳事件給eventTextAreaCallback,但程式的部分今天先不講。

以上都做完之後,外型的部分差不多就設計完成了。

Dernière modification le 1517213520000
Shuangyue
« Citoyen »
1535878320000
    • Shuangyue#0000
    • Profil
    • Derniers messages
#6
  0
震惊
收藏一下
Iamchinese
« Citoyen »
1672322220000
    • Iamchinese#3732
    • Profil
    • Derniers messages
    • Tribu
#7
  0
学废了,关于!img这件事
  • Forums
  • /
  • Transformice
  • /
  • Modules
  • /
  • 【Lua教學】基礎Lua
© Atelier801 2018

Equipe Conditions Générales d'Utilisation Politique de Confidentialité Contact

Version 1.27