ConstructorRichi's Forum Posts

  • GeorgeZaharia Can you give us an example?

  • Hello,

    every script you create in the Construct 3 Scripts Folder is accessable everywhere in the runtime. So you could do something like this on your "Startscript".

    1. Create a global variable to store the current active layout.
    2. Add an event listener and a corresponding function to every layout.
    3. In the Tick function call your layout Tick functions and check if the current layout is active.
    4. The onbeforelayoutstart functions and the specific Tick functions are in their corresponding layout script files.
    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);
    	if(g_currentLayout == "Layout 2")Tick_layout2(runtime);
    }
    

    Here is a possible project structure:

    On every layout script page you now have:

    function onBeforeLayoutStart_Layout1(runtime){
    	
    }
    
    function Tick_layout1(runtime){
    
    }
    

    Hope this helps.

  • If you only need an on click event for your Objects. I would use a normal sprite and add an event listener to it.

    Here you can find an example.

    Why would you use a button for that?

  • Ok, i found a solution using async functions. Now you can use "mySleep(time in ms)" in every async function in your project. The only addition is, that you need a check, that the async function is only called once (see global variable: "g_active"):

    // global variable, could also be stored in an object
    let g_active = true;
    
    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;
    }
    

    What do you think?

  • Hello,

    i would like to create a turn based battle system. I already created one with Events, but there i use this events with the "wait" events:

    How can i accomblish this with js? The js function "Sleep()" is not working. Ashley said that you could use this function to wait until something happens next:

    setTimeout(() => {
    			console.log("Code to run after 1 second");
    		}, 1000);
    

    But when i want to chain some waits, i need to nest "setTimeout()s"? Is there a better solution, or an example of a turn based battle function?

    Thank you!

  • Thanks again. Here the solution Ashley mentioned:

    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");
    		}
    	}
    }
    
  • programmer__r: If you would like to get clicks on a sprite, i think you need to use "containsPoint(x, y)". Here is an example:

    I added the two sprites to a family and add a "mousedown" event. On every click it will loop through the family children and checks, if a child is under the current mouse coordinates:

    async function OnBeforeProjectStart(runtime)
    {
    	// add corresponding txtFields UID and a counter variable to instances of sprites
    	runtime.objects.Sprite1.getFirstInstance().txtUID = runtime.objects.txtSprite1.getFirstInstance().uid;
    	runtime.objects.Sprite2.getFirstInstance().txtUID = runtime.objects.txtSprite2.getFirstInstance().uid;
    	runtime.objects.Sprite1.getFirstInstance().myCounter = 0;
    	runtime.objects.Sprite2.getFirstInstance().myCounter = 0;
    	
    	// add an EventListener mousedown
    	runtime.addEventListener("mousedown", e => OnMouseDown(e, runtime));
    	
    	runtime.addEventListener("tick", () => Tick(runtime));
    }
    
    
    function OnMouseDown(e, runtime){
    	// call click functions, here you could filter wich function you want to test
    	SpritesClicked(e, runtime);
    }
    
    function SpritesClicked(e, runtime){
    	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(e.clientX, e.clientY)){
    			sprites[i].myCounter += 1;
    			runtime.getInstanceByUid(sprites[i].txtUID).text = "Clicks: " + sprites[i].myCounter.toString();
    		}
    	}
    }
    

    I think this is the way you would simulate the "Mouse on Object clicked Event".

  • Now it works, thank you:)

    runtime.objects.Button.getFirstInstance().addEventListener("click", () => {
    		console.log("Hello World!");
    	}
    
  • Hello,

    when do you call your "intializeCareerIndices()" function?

    I created a global variable "Test=15" in an event sheet and call this code and it returns 15 as it should do:

    async function OnBeforeProjectStart(runtime)
    {
    	
    	runtime.addEventListener("tick", () => Tick(runtime));
    	
    	var careerIndices = [];
    	for (var i = 0; i < runtime.globalVars.Test; i++) {
    		careerIndices.push(i);
    	}
    	console.log(careerIndices.length);
    }
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Is this only possible with this? Or can an object get an event listener "addEventListener("click", ...)?

    containsPoint(x, y)

    Test if a point intersects this instance, using its collision polygon if any, and return a boolean indicating if the point is inside the instance's collision area.

    Here you can find an example.

  • Hello Igordias,

    this is not working. I added a Button instance and this code. Nothing happens...

    runtime.objects.Button.addEventListener("click", () => {
    		console.log("Hello World!");
    	}
    );
    

    What do i missing?

  • Hello,

    should i group i.e. enemy objects to a familiy like i would do with the event editor?

    Right now i extend the ISpriteInstance to add specific variables and methods directly to an enemy object.

    class EnemyInstance extends ISpriteInstance {
    
    constructor(){
    	super()
    	this.name = "Zombie";
    	this.health = 50;
    	this.direction = "";
    }
    Move(){...
    }
    

    When i have lots of different enemy objects, and every enemy should have a Move() method, i think it would be better to extend a "Family" class, so every child of the familiy will inherit the Move() method.

    Is this possible?

    Thank you

    Tagged:

  • Hello,

    i have a 3 dimensional array like this:

    {
    "c2array":true,
    "size":[3,3,3],
    "data":[
    	[
    		["Students"],
    		["Simon"],
    		["Sarah"]
    	],[
    		["Proben"],
    		["Brüche zu Dezimalbrüchen 1","Mathe","Dezimalbrüche"],
    		["Geometrische Flächen","Mathe","Geometrie"]
    	],[
    		["Themen"],
    		["Dezimalbrüche","Mathe"],
    		["Geometrie","Mathe"]
    	]
    ]
    }
    [/code:2lualsp3]
    
    How can i get the "depth" of i.e. "Geometrische Flächen"
    or
    how can i get the "height" of i.e. "Students" ?
    
    Is there a possibility to do sth. like this: myArray.At(0).lengthor myArray.At(1,1).length.
    
    Thank you
  • Hello,

    i tried to post a feedback to the tutorial: "Plugins - roll your own with the JavaScript SDK", but when i click "Publish" i get an error: "Oops! Something went wrong! Please try again or email support."

    Is this a bug, or do i don´t have the permission to post feedbacks?

    Thank you

  • Hello,

    does anybody have an updated tutorial for creating a simple plugin?

    The "https://www.scirra.com/tutorials/352/plugins-roll-your-own-with-the-javascript-sdk" Tutorial doesn´t work with the current version, because of the .draw function call in the "instanceProto.drawGL = function (glw)"!

    As i heard, we should only use the drawGL function, but is there an example? My test was:

    ctx.strokeStyle="#FF0000";
    ctx.strokeRect(20,20,150,100);[/code:hx0youwk]
    in the .draw function -> how do i adapt this to the drawGL?
    
    Thanks in advance