Test Overlap not working

0 favourites
  • 10 posts
From the Asset Store
2D fighting template based in the game that defined the fighting games genre.
  • Hey there, I have been using javascript for about two years now and I just started using it in C3. I cannot get something to happen when two objects collide.

    const player = runtime.objects.Player.getAllInstances();
    const ground = runtime.objects.Ground.getAllInstances();
    	if (player.testOverlap(ground)) {
    	console.log('Hit')
    	}
    

    I don't know if I'm doing something wrong or it's just a typo but please help me.

  • Hello, this bit will surely not work:

    player.testOverlap(....
    

    As in this case player is a reference to an array of objects, not to a specific sprite object.

    See getAllInstances() on: construct.net/en/make-games/manuals/construct-3/scripting/scripting-reference/object-interfaces/iobjectclass

    Return an array of all instances of this object class.

    You need to find your given sprite object in the array. If you have only one player, then player.getFirstInstance() should do it. You also need to do the same to find the given ground object in your ground array.

    One way to look for a specific object in an array can be seen in this post:

    construct.net/en/forum/construct-3/scripting-51/handle-clicks-object-js-154194

    In that example, in the function the // loops through all sprites for loop is used to get a reference to each sprite in the array.

    If you - for example - have an instance variable named myID added to the sprite in the Editor, then in the JS for loop you can get its value like this:

    const myID = sprites[i].instVars.myID;
    console.log(myID);
    

    Hope this helps.

  • Ok, now I'm really lost. Can you please give me a code snippet or something of a basic collision test?

    And also, is it supposed to be kind of like this?...

    function Tick(runtime)
    {
    const player = runtime.objects.Player.getFirstInstance()
    const ground = runtime.objects.Ground.getFirstInstance()
    const sprites = runtime.objects
    	for(let i = 0; i === sprites.length; i++) {
    	if (sprites[i].uid === 2) {
    	if (player.testOverlap(ground)) {
    	console.log('Hit')
    			}
    		}
    	}
    }
  • Remember I have never programed javascript in C3. Only in other platforms. LOL I'm not used to using JS in C3. lol sorry tho if I look dumb for trying.

  • So if you only have one ground object and one player object you should just be able to do this:

    	const player = runtime.objects.Player.getFirstInstance();
    	const ground = runtime.objects.Ground.getFirstInstance();
    	
    	if (player.testOverlap(ground)) {
    		console.log('Hit');
    	}
    

    If you have many ground objects and one player object you can do this:

    	const player = runtime.objects.Player.getFirstInstance();
    	const groundObjects = runtime.objects.Ground.getAllInstances();
    	// loop through every ground object and see if any of them overlap with the player
    	groundObjects.forEach((ground) => {
    		if (player.testOverlap(ground)) {
    			console.log('Hit');
    		}
    	});
    	
    

    And if you have many ground objects and many player objects you'd need to something like this:

    	const playerObjects = runtime.objects.Player.getAllInstances();
    	const groundObjects = runtime.objects.Ground.getAllInstances();
    	// loop through every player object and every ground object and see if any of them overlap with the player
    	playerObjects.forEach((player) => {
    		groundObjects.forEach((ground) => {
    			if (player.testOverlap(ground)) {
    				console.log('Hit');
    			}
    		});
    	});
    

    Hopefully that makes sense! Basically "getAllInstances" returns an array, or list, of your objects and you need to loop through each of them and test if they overlap with your player individually. "getFirstInstance" just returns one of the objects, the first one in that list - which is fine if you only have one of that particular object(such as a player object)

  • Bro. This isn't working. I have 1 player obj and 1 ground obj. I'm placing the code under tick to happen every tick.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Felixoo no idea why, that looks like it should be working. I've recreated pretty much exactly what you've done here and it's working fine, if you want to take a look:

    edwardbonnett.com/script-test.c3p

  • Yeah, idk why it's not working. I even completely remade the project but it still didn't work. ):

  • Maybe somebody can find an error in this:

    function Tick(runtime)
    {
    	const player = runtime.objects.Player.getFirstInstance();
    	const ground = runtime.objects.Ground.getFirstInstance();
    	if (player.testOverlap(ground)) {
    		player.y = 0;
    	}
    	
    }
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)