跳轉到內容

Celestia/Celx 指令碼/問答

來自華夏公益教科書

此頁面旨在作為 CELX 指令碼實踐問題和解答 (Q&A) 的儲存庫。有兩種方法可以將內容列在這裡

- 只需在下面的 Q&A 部分列出您的通用問題,就會有人在其中釋出答案。請將有關特定答案的討論(例如澄清請求)釋出到該答案的討論頁面。然而不幸的是,很少有 CELX 程式設計專家定期瀏覽此華夏公益教科書。

或(首選)

- 在 Celestia 指令碼論壇 上釋出您的問題。一旦您在那裡對答案感到滿意,請將問題和最終答案摘要轉錄到這裡,以便我們可以在此華夏公益教科書中建立一個知識庫。

如何計算兩個物體之間的角度?

[編輯 | 編輯原始碼]

回答

如何將 CELX 函式分配給鍵盤上的特定鍵?

[編輯 | 編輯原始碼]

回答


以下的疑問和解答已從一個“討論”頁面遷移到這裡。那些頁面用於討論與編輯華夏公益教科書頁面相關的議題,而不是提供華夏公益教科書的內容。

如果有任何不清楚的地方,請在這裡提問。

只需點選上面“編輯此頁面”按鈕,然後開始輸入。或者點選此頁面中的一個[編輯]按鈕,並在其底部插入一個新的問題或解答。


測試指令碼和問題

[編輯 | 編輯原始碼]

使用這裡第一個非常簡單的 CELX 示例指令碼,即

celestia:flash("Hello, Universe!")
wait(2.0)

"flash" 函式在 "Celestia" 物件中定義。當此指令碼執行時,我希望它除了顯示文字 "Hello Universe!" 外不做任何事情。

在執行此指令碼之前,我已經將地球居中在我的 Celestia 視窗中,並且處於“跟隨”模式。當我執行此指令碼時,視點會發生變化,使地球在看到 "Hello Universe!" 訊息之前被移動到一邊或另一邊。

我是否正確地認為這種移動是由 Celestia 對 "flash" 函式的定義引起的?換句話說,Celestia 本身中的 "flash" 函式可能包括一些要執行的移動函式。是的?

不,當我嘗試這個時,我沒有看到地球移動。你是如何執行指令碼的?



在研究了 Celestia 中的 "flash" 和 "print" 函式的屬性之後,我嘗試了以下指令碼將訊息居中在我的顯示屏上。這也導致了地球的移動,即使我認為我將所有內容居中在螢幕上,因為我在引數列表中使用了 0(零)。有人能解釋一下這裡發生了什麼嗎?

celestia:print("Hello Universe!", 5, 0, 0, 0, 0)
wait (5)

我也沒有看到這個。可能與上面的問題一樣。
謎團解開...開啟指令碼對話方塊,雙擊 celx 指令碼以開啟它。如果我只是使用 Celestia 提供的對話方塊開啟指令碼,則不會出現移動。謝謝。
換句話說,不要雙擊檔名。只需點選開啟對話方塊。
還有一點...如果我使用 CEL 指令碼這樣做,則對 CEL 指令碼沒有明顯的影響。

如何在 Debian GNU/Linux(celestia 1.6.1)上執行 celx 指令碼?我嘗試使用 $ celestia --url test.celx,並收到無效選項 "--" 的提示。

Linux 上的 Celestia 目前不支援 --url 引數。Celx 指令碼可以使用引數 "-f /path/to/script.celx" 執行。請注意,指令碼路徑必須是完全顯式的路徑,而不是相對路徑。請參閱 Debian 錯誤 #682258

來自 BrainDead 的測試指令碼

[編輯 | 編輯原始碼]
  flashRadius = function (bodyName)
  body = celestia:find(bodyName) -- finds object 
  radius = body:radius() -- gets radius
  celestia:flash("The radius of " .. body:name() .. " is " .. radius .. " km.")
  wait(2.0)
  end
     
  flashRadius("Mercury")
  flashRadius("Venus")
  flashRadius("Earth")
  flashRadius("Mars")
  flashRadius("Jupiter")
  flashRadius("Saturn")
  flashRadius("Uranus")
  flashRadius("Neptune")
  flashRadius("Pluto")
  flashRadius("Charon")

  function flash(message)
  celestia:flash(message)
  wait(2.0)
  end

  message = "End of this test script"
  flash = celestia:flash(message,5)
上面一個重要的點...變數 "message" 必須在
執行 "flash" 函式之前定義,否則指令碼會引發錯誤,提示
它需要一個字串。這是有道理的。我仍在學習。
但這一點讓我很困惑。如果 "flash" 函式已定義,
這個指令碼是如何顯示文字 "End of this test script" 的,
但它沒有被呼叫?換句話說,為什麼我不需要
呼叫函式的一行?即 - flash(message)
你沒有呼叫 "flash",但你確實呼叫了 "celestia:flash"。語句
   flash = celestia:flash(message,5)
呼叫 "celestia" 物件的 "flash" 方法(這就是賦值右側的含義),然後將結果 (nil) 賦值給變數 "flash",重新定義它。這可能不是你想要的。只有在定義函式時,你才會對 "flash" 變數進行賦值。請記住
   function flash(message)
   celestia:flash(message)
   wait(2.0)
   end
等同於
 flash =  function (message)
   celestia:flash(message)
   wait(2.0)
   end
函式名 "flash" 只是另一個變數。 HankR 17:59, 2005 年 10 月 18 日 (UTC)
好的,我認為我理解你的意思。感謝你的幫助。--BrainDead 22:45, 2005 年 10 月 18 日 (UTC)

另一個測試指令碼

[編輯 | 編輯原始碼]
   camera = celestia:getobserver()
   tethys = celestia:find("Sol/Saturn/Tethys") -- finds object named Tethys.
   celestia:select(tethys) 
   camera:center(tethys,5)
   wait(5)
   camera:follow(tethys)
   cameraPosition = camera:getposition() -- gets position of observer.
   tethysPosition = tethys:getposition() -- gets position of Tethys.
   distance = tethysPosition:distanceto(cameraPosition) -- gets distance from position of Tethys to observer.
   celestia:flash("Current distance to Tethys is "..distance.. " km",5)
   wait(5)
   celestia:flash("Let's have a look...",5)
   wait(5)
   obs=celestia:getobserver()
   obs:gotodistance(tethys,10000,5)
   wait(5)
   celestia:flash("Here it is!",5)
   wait(5)
   newPosition = obs:getposition() -- gets NEW position of the observer.
   newdistance = tethysPosition:distanceto(newPosition) -- gets distance from position of Tethys to new observer position.
   celestia:flash("Current distance to Tethys is "..newdistance.. " km",5)
   wait(5)
好的,這個可以執行,但我還有問題...首先,感謝 Malenfant 的這個想法。
如何對 tethysPosition 和 newdistance 顯示的值進行舍入?
使用 <"Current distance to Tethys is "..string.format("%i",newdistance).." km"
為什麼 newdistance 值既沒有顯示 10000km,也沒有顯示 Celestia 顯示屏上的值?
Celestia 的顯示屏顯示的是到表面的距離,你計算的是中心到中心的距離。此外,土衛六在你獲取其位置的時間後已經移動了。你計算的是到其舊位置的距離,而不是到其當前位置的距離。你需要再次獲取位置。每當呼叫 wait() 時,時間都會過去,事物也會移動。 HankR 03:31, 2005 年 10 月 24 日 (UTC)
我還在努力。--BrainDead
再次感謝你的幫助,Hank。我現在理解了這一點。--BrainDead 10:46, 2005 年 10 月 24 日 (UTC)

修正的指令碼

[編輯 | 編輯原始碼]
   myobject = celestia:find("Sol/Saturn/Tethys") -- finds object named Tethys.
   celestia:select(myobject) 
   celestia:center(myobject,5)
   wait(5)
   celestia:follow(myobject)
   camera = celestia:getobserver()
   cameraPosition = camera:getposition() -- gets position of observer.
   tethysPosition = myobject:getposition() -- gets position of Tethys.
   distance = tethysPosition:distanceto(cameraPosition) -- gets distance from  position of Tethys to observer.
   celestia:flash("Current distance to Tethys is "..string.format("%i",distance).." km",5)
   wait(5)
   celestia:flash("Let's have a look...",5)
   wait(5)
   celestia:gotodistance(tethys,10000,5)
   wait(5)
   celestia:flash("Here it is!",5)
   wait(5)
   obs=celestia:getobserver()
   newPosition = obs:getposition() -- gets NEW position of the observer.
   newdistance = tethysPosition:distanceto(newPosition) -- gets distance from position of Tethys to   new observer position.
   celestia:flash("Current distance to Tethys is "..string.format("%i",newdistance).." km",5)
   wait(5)
此指令碼修訂版糾正了我之前犯的錯誤,並反映了一個整數值。
不過,我想請問您,您是如何知道需要更正為整數值的?我在任何
我擁有的文件中都找不到。
在 Lua 5.0 參考手冊中。
您在哪裡找到 "..string.format("%i")..",我可以用它來顯示兩位小數嗎?也許
使用 "..string.format("%2")???? --BrainDead 00:33, 2005年10月25日(UTC)
string.format 使用 C 風格的輸出格式控制字串。使用 "%.2f" 來顯示兩位小數。
PS - 我這裡使用的格式可以嗎?我是否不應該釋出這麼多指令碼?我應該使用不同的
標題嗎?抱歉,只是我… 困惑的鮑勃
如果將這些內容重新格式化為 CELX 的常見問題解答頁面,可能會很有幫助。將每個問題放在一個單獨的部分中,並將問題作為部分標題。這樣會使它對其他人更有用。HankR 01:05, 2005年10月25日(UTC)



華夏公益教科書