Code Snippets

Inhalt
Überprüft, ob ein Sprite Objekt mit der Maus angeklickt wurde:
async function OnBeforeProjectStart(runtime)
{
    runtime.addEventListener("mousedown", e => OnMouseDown(e, runtime));
    runtime.addEventListener("tick", () => Tick(runtime));
}

function OnMouseDown(e, runtime){
    // get the actual layout with first layer
    const currentLayer = runtime.layout.getLayer(0);
    const mouseXYAr = currentLayer.cssPxToLayer(e.clientX, e.clientY, 0);
    const sprites = runtime.objects.sprites.getAllInstances();

    // loops through all sprites
    for(var i = 0; i < sprites.length; i++) {
        // checks if sprite contains mouse x,y point
        if(sprites[i].containsPoint(mouseXYAr[0], mouseXYAr[1])){
            console.log("spirte clicked");
        }
    }
}
async mySleep() Methode:
let g_active = true;

runOnStartup(async runtime =>
{
    runtime.addEventListener("beforeprojectstart", () => OnBeforeProjectStart(runtime));
});

async function OnBeforeProjectStart(runtime)
{
    runtime.addEventListener("tick", () => Tick(runtime));
}

function Tick(runtime)
{
    if(g_active) demoFunction();
}

function mySleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

async function demoFunction() {
    g_active = false;
    console.log('start');
    await mySleep(2000);
    console.log('two seconds later');
    await mySleep(2000);
    console.log('four seconds later');
    await mySleep(2000);
    g_active = true;
}
Audio Dateien abspielen mit dem AudioManager.js:

Zuerst muss man dem Projekt die Javascript Datei "AudioManager.js" hinzufügen.

// globale Variablen:
// sound files
let g_audioManager = null;
let g_audioAttack1 = null;

async function OnBeforeProjectStart(runtime)
{
    g_LoadSounds(runtime);
}

async function g_LoadSounds(runtime){
    g_audioManager = new AudioManager(runtime);

    [g_audioAttack1] = await Promise.all([
        g_audioManager.loadSound("attack1.webm")
    ]);
}
  • Alle Audio Dateien in Consctruct 3 benötigen die Dateiendung ".webm", welche autoamtisch erzeugt wird, wenn man eine Audio File importiert. Dazu klickt man mit der rechten Maustaste auf den "Sounds"-Ordner und wählt "Import Sounds" aus.
Tick und Onbeforelayoutstart Funktionen für jedes Layout:
let g_currentLayout = "Layout 1";

runOnStartup(async runtime =>
{
    runtime.addEventListener("beforeprojectstart", () => OnBeforeProjectStart(runtime));
});

async function OnBeforeProjectStart(runtime)
{

    runtime.getLayout("Layout 1").addEventListener("beforelayoutstart", () => onBeforeLayoutStart_Layout1(runtime));
    runtime.getLayout("Layout 2").addEventListener("beforelayoutstart", () => onBeforeLayoutStart_Layout2(runtime));

    runtime.addEventListener("tick", () => Tick(runtime));
}

function Tick(runtime)
{
    if(g_currentLayout == "Layout 1")Tick_layout1(runtime);
    else if(g_currentLayout == "Layout 2")Tick_layout2(runtime);
}

// die Funktionen Tick_layout1 und onBeforeLayoutStart_Layout1 sind in eigenen .js Dateien:
function onBeforeLayoutStart_Layout1(runtime){

}

function Tick_layout1(runtime){

}