JavaScript Mod:修订间差异

来自像素工厂维基百科-mindustry wiki
无编辑摘要
第38行: 第38行:
=== 一些小坑 ===
=== 一些小坑 ===
一个函数接受所有方法,例如:
一个函数接受所有方法,例如:
 
extend(BulletType, {
* <code>extend(BulletType, {</code>
  hit(b, x, y) { print(b); }
*  <code>'''hit(b, x, y) {''' '''print(b);''' '''}'''</code>
}
* <code>}</code>
 
<code>BulletType#'''hit'''('''Bullet''' b)</code>和<code>BulletType#'''hit'''('''Bullet''' b, '''float''' x, '''float''' y)</code>都会调用上面的hit方法,
<code>BulletType#'''hit'''('''Bullet''' b)</code>和<code>BulletType#'''hit'''('''Bullet''' b, '''float''' x, '''float''' y)</code>都会调用上面的hit方法,


所以<code>x</code>,<code>y</code>可能为<code>undefined</code>
所以<code>x</code>,<code>y</code>可能为<code>undefined</code>

2024年2月18日 (日) 12:15的版本

Mindustry的JS

Mindustry使用: Rhino

类似于node.js,它没有windowdocument等一系列变量

[菜鸟教程] | [MDN]

Mindustry提供了一下全局变量:global.js

一些常用的变量

  • scriptName: 当前脚本的名称(这会随脚本的变化而变化,建议: const { scriptName } = this;
  • modName: 当前Mod的名称(这会随mod的变化而变化,建议: const { modName } = this;
  • print: 打印日志
  • extend: js 'extend(Base, ..., {})' = java 'new Base(...) {}'

关于解构复制

cosnt { a, b } = { a: 1, b: 2 };

特殊语法

new xxx/* functionName */ ( /* args */ ) { /* code */ }

其实就是xxx/* functionName */ ( /* args */ , { /* code */ } )

在最后面传了一个对象

所以你可以:new extend(Block, "a block") {

setBars() { print(this.barMap) }

}

Getter & Setter

你可以使用xxx.class等价于xxx.getClass()

  • 注意:Vars.class获取到的是Class类,如果想要获取对应类对象 可以使用Vars.__javaObject__

一些小坑

一个函数接受所有方法,例如:

extend(BulletType, {
  hit(b, x, y) { print(b); }
}

BulletType#hit(Bullet b)BulletType#hit(Bullet b, float x, float y)都会调用上面的hit方法,

所以xy可能为undefined