匿名
未登录
创建账号
登录
Mindustry中文wiki
搜索
查看“官方维基教程-脚本编写”的源代码
来自Mindustry中文wiki
命名空间
页面
讨论
更多
更多
页面操作
阅读
查看源代码
历史
←
官方维基教程-脚本编写
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于这些用户组的用户执行:
管理员
、editor
您可以查看和复制此页面的源代码。
= 脚本编写 = Mindustry 使用 JavaScript 作为脚本编写的语言。 Mindustry 使用的 JavaScript 基于 Rhino。 所以你可以使用 Java 的类。 例如: <pre lang="js">java.lang.System.out.println("this is a test!"); </pre> 脚本使用 <code>js</code> 的拓展名,被放置在 <code>scripts/</code> 文件夹。 脚本的执行开始于名为 <code>main.js</code> 的文件夹,其他脚本可以用<code>require("脚本名")</code>语句导入主文件。典型的设置脚本如下所示: ''scripts/main.js'': <pre lang="js">require("blocks"); require("items"); </pre> ''scripts/blocks.js'': <pre lang="js">const myBlock = extend(Conveyor, "terrible-conveyor", { // 各种字段... size: 3, health: 200 //... }); </pre> ''scripts/items.js'': <pre lang="js">const terribleium = Item("terribleium"); terribleium.color = Color.valueOf("ff0000"); //... </pre> = 示例 = == 监听事件 == <pre lang="js">// 监听单位被摧毁的事件 Events.on(UnitDestroyEvent, event => { // 如果单位是玩家在屏幕上方显示文字 if(event.unit.isPlayer()){ Vars.ui.hudfrag.showToast("Pathetic."); } }) </pre> 找到能监听的事件最简单的方法就是看源码: [https://github.com/Anuken/Mindustry/blob/master/core/src/mindustry/game/EventType.java Mindustry/blob/master/core/src/mindustry/game/EventType.java] == 显示对话框 == <pre lang="js">const myDialog = new BaseDialog("Dialog Title"); // 加入“关闭”按钮 myDialog.addCloseButton(); // 在主体中添加文字 myDialog.cont.add("Goodbye."); // 显示对话框 myDialog.show(); </pre> == 播放自定义声音 == 播放自定义声音很简单,你只需要提供在 <code>/sounds</code> 里存储的<code>.mp3</code> 或者 <code>.ogg</code>文件。 在这个例子中,我们存储了 <code>example.mp3</code> 在 <code>/sounds/example.mp3</code> 。 === 使用链接库播放声音 === ''scripts/alib.js'': <pre lang="js">exports.loadSound = (() => { const cache = {}; return (path) => { const c = cache[path]; if (c === undefined) { return cache[path] = loadSound(path); } return c; } })(); </pre> ''scripts/main.js'': <pre lang="js">const lib = require("alib"); Events.on(WaveEvent, event => { // 加载 example.mp3 const mySound = lib.loadSound("example"); // 游戏引擎将会在 (X,Y)处生成声音 mySound.at(1, 1); }) </pre> //TODO test these out and add more examples
返回
官方维基教程-脚本编写
。
导航
导航
塞普罗
-- 战役区块
-- 物品
-- 流体
-- 建筑
-- 单位
埃里克尔
-- 战役区块
-- 物品
-- 流体
-- 建筑
-- 单位
隐藏
-- 物品
-- 液体
-- 建筑
-- 地块介绍
其他
-- 状态效果
-- 天气
-- 星球
教程中心
编辑者
-- 编辑指导
-- 语法指南
-- 表格语法
最近更改
随机页面
MediaWiki帮助
wiki工具
wiki工具
特殊页面
页面工具
页面工具
用户页面工具
更多
链入页面
相关更改
页面信息
页面日志