runOnStartup(async runtime =>
{
// This code runs on startup with
// the 'runtime' variable available
});
runOnStartup(async runtime =>
{
runtime.addEventListener("beforeprojectstart", () => OnBeforeProjectStart(runtime));
runtime.addEventListener("afterprojectstart", () => OnAfterProjectStart(runtime));
});
function OnBeforeProjectStart(runtime)
{
runtime.addEventListener("tick", () => OnTick(runtime));
runtime.addEventListener("keydown", e => OnKeyDown(e,runtime));
runtime.addEventListener("keyup", e => OnKeyUp(e,runtime));
runtime.addEventListener("mousedown", e => OnMouseDown(e,runtime));
runtime.addEventListener("mousemove", e => OnMouseMove(e,runtime));
runtime.addEventListener("mouseup", e => OnMouseUp(e,runtime));
runtime.addEventListener("dblclick", e => OnMouseDoubleClick(e,runtime));
runtime.addEventListener("wheel", e => OnMouseWheel(e,runtime));
runtime.addEventListener("pointerdown", e => OnPointDown(e,runtime));
runtime.addEventListener("pointermove", e => OnPointMove(e,runtime));
runtime.addEventListener("pointerup", e => OnPointUp(e,runtime));
runtime.addEventListener("pointercancel", e => OnPointCancel(e,runtime));
runtime.addEventListener("deviceorientation", e => OnDeviceOrientation(e,runtime));
runtime.addEventListener("devicemotion", e => OnDevicemotion(e,runtime));
runtime.addEventListener("save", e => OnSave(e,runtime));
runtime.addEventListener("load", e => OnLoad(e,runtime));
runtime.addEventListener("instancecreate", e => OnInstanceCreate(e,runtime));
runtime.addEventListener("instancedestroy", e => OnInstanceDestroy(e,runtime));
}
function Tick(runtime)
{
// Code to run every tick
}
用脚本获取全局变量。直接对它进行赋值操作也是可以的。
runtime.globalVars.Variable1
在事件条范围内,用脚本获取本地变量。备注,函数的传入参数(`Function parameter`) 也是通过它来获取。
localVars.Variable2
提示,在某些情况下,变量名称可能不是有效的标识符,例如可能跟保留关键字冲突了。此时你还可以用字符串来引用这个变量。
runtime.globalVars["Variable1"]
localVars["Variable2"]
instVars["Variable3"]
runtime.objects.Sprite.getFirstInstance()
返回的是一个数组。
runtime.objects.Sprite.getAllInstances()
const player = runtime.objects.Sprite.getFirstInstance();
const playerBehavior = player.behaviors.Platform;
// console.log(playerBehavior.maxSpeed)
在某些情况下,有些名称可能不是有效的标识符,此时,还可以用字符串来引用。
instance.behaviors["8Direction"]["maxSpeed"]
遍历所有 Sprite 对象的实例变量 Score,同时销毁所有变量小于 60 的实例。
for (const obj of runtime.objects.Sprite.instances())
{
const score = player.instVars["Score"];
if (score <= 60)
{
obj.destroy();
}
}
在某些情况下,有些名称可能不是有效的标识符,此时,还可以用字符串来引用。
const textObject = runtime.objects.Text;
const textInstance = textObject.getFirstPickedInstance();
textInstance.text = "Hello World"
也可以通过 filter来实现条件筛选。
例如下方的代码,用于选出布尔型 foobar 是开启状态下 obj 实例对象。
const instances = runtime.objects.Sprite.getAllInstances(); // get an array of instances
const picked = instances.filter(obj => obj.instVars.foobar); // filter based on instance Variable
再来一个示例
// 获取所有 Sprite 实例
const instances = runtime.objects.Sprite.getAllInstances();
// 使用筛选逻辑过滤实例
const picked = instances.filter(obj => {
// 获取每个实例的分数
const score = obj.instVars["Score"];
// 返回分数小于或等于60的实例
return score <= 60;
});
const textObject = runtime.objects.Text;
const textInstance = textObject.getFirstPickedInstance();
textInstance.text = "Hello World"
}
例如,事件表里有一个自定义函数 add ,如何用js来调用它
调用函数
runtime.callFunction("add")
获取函数的参数
localVars.Parameter0
设置函数的返回值
runtime.setReturnValue();
在脚本中代替事件表AJAX加载文件的操作。
async function OnBeforeProjectStart(runtime)
{
const InfoData = await runtime.assets.fetchJson('info.json')
runtime.objects.JSON.getFirstInstance().setJsonDataCopy(InfoData);
}
}
const dataInst = runtime.objects.JSON.getFirstInstance();
const data = dataInst.getJsonDataCopy();
console.log(data['Name'])
const dataInst = runtime.objects.JSON.getFirstInstance();
const data = dataInst.getJsonDataCopy();
data.name = localVars.name;
dataInst.setJsonDataCopy(filterItems);
const MyArray = runtime.objects.Dictionary.getFirstInstance();
console.log(MyArray.getAt(0, 0))
const MyData = runtime.objects.Dictionary.getFirstInstance().getDataMap();
console.log(MyData.get('Key'))
c3ArrayFile
,用于从在 AJAX 加载 Array文件时,将 AJAX.LastData 数据传入到这个 Function 中。
Function 具有 String 类型的返回值,传出的是一个可被解析为 JSON 的字符串,用于在 JSON 对象中使用 Parse JSON string 动作时,使用 Functions.convertC3ArrayToJson(AJAX.LastData) 进行解析。
const { size, data } = JSON.parse(localVars.c3ArrayFile);
const [, totalRows] = size;
let output = [];
const columnHeaders = data.map(column => column[0][0].trim());
for (let rowIndex = 1; rowIndex < totalRows; rowIndex++) {
let rowObject = {};
columnHeaders.forEach((header, columnIndex) => {
if (!header) return;
let cellValue = data[columnIndex][rowIndex][0];
rowObject[header] = typeof cellValue === 'string' ? cellValue.trim() : cellValue;
});
if (Object.values(rowObject).some(value => value !== "")) {
output.push(rowObject);
}
}
runtime.setReturnValue(JSON.stringify(output));