Skip to content
This repository has been archived by the owner on Jan 2, 2023. It is now read-only.

Script API 2: Script Examples

lunarcleint edited this page Dec 30, 2022 · 5 revisions

Script Examples

Basic Script Fuctions

function new() {}
function update(elapsed:Float) {}
function destroy() {}

trace("loaded script :)");

Parsing a Json

var jsonPath:String = "assets/scripts/data.json";
/*
	data.json
	{
	"test": 3,
	"fr": 4
	}
 */
 
function new()
{
	var json:Dynamic = {};

	if (FileSystem.exists(jsonPath))
		json = Json.parse(File.getContent(jsonPath));

	trace(json); // { fr => 4, test => 3 }
}

Adding and getting a script:

  • util.hx:
var someUtilVar:String = "Hello! :))";

function someUtilFunc(a, b):Int {
    return a + b;
}
  • script.hx:
addScript("util");

var util = getScript("util");

trace(util.someUtilFunc(1,1)); // prints 2
trace(util.someUtilVar); // prints Hello! :))

Boyfriend goes fucking insane

//
import("flixel.addons.effects.FlxTrail");


var trail:FlxTrail = null;
var defaultBfPos:Array<Int> = null;

function create()
{
	trail = new FlxTrail(boyfriend, null, 32, bpm / 4, 0.3, 0.01);
	boyfriendGroup.insert(boyfriendGroup.members.indexOf(boyfriend), trail);

	defaultBfPos = [
		boyfriend.x,
		boyfriend.y
	];
}

function update(elapsed:Float)
{
	var currentBeat = (Conductor.songPosition / 1000) * (bpm / 60);
	boyfriend.x = defaultBfPos[0] + 60 * Math.sin((currentBeat * 0.70) * Math.PI);
	boyfriend.y = defaultBfPos[1] + 60 * Math.cos((currentBeat * 0.70) * Math.PI);
	
	PlayState.instance.moveCameraSection();
}