This forum is read only and just serves as an archive. If you have any questions, please post them on

1 decade ago by TrexKikBut

I want to get the distance from 1 entity to the other, because when one entity is near by, i want it to shoot an entity..

How could I do this?

1 decade ago by lazer

You can use the .distanceTo() method (

I recently posted a code snippet that uses it in my procedural level experiment, to cycle through an array of entities and find the distance to each of them, then kill the current entity if it's too close to any of the others:

Example code from post:

var allPits = 'EntityPit1' );
// Get distance from this pit to every other pit
        for (var i = 0, otherPit; otherPit = allPits[i]; i++) {
            var distanceToPit = this.distanceTo(otherPit);
            // If this pit is less than 1 pit's width apart from another pit,
            if (distanceToPit <= this.size.x * 2) {
                // Kill this pit

1 decade ago by TrexKikBut

I'm also wondering how to find out how many ents of the type are left..

I'm trying this, I want to count how many EntityZombies are in the level.

var zombieCount =[0];

1 decade ago by lazer

Disclaimer: I'm no JS expert, so I could be totally wrong, but here's what I think is happening:

What you're doing above is getting just the first element in the array of Zombies (by specifying that you're getting the element at position [0] in the array). You need to get all of them, not just the first one, and then get that array's length to count how many there are.

For example, here in my code I'm pulling out all Pit1 entities that are currently in existence:

var allPits = 'EntityPit1' );

If I wanted to find out how many of these entities there are currently in existence I'd use .length:

var pitCount = allPits.length;

And this should, theoretically, give me the number of pits by counting the number of elements in the allPits array. You should be able to do the same thing for your zombies.

1 decade ago by TrexKikBut

Another thing, how can I call a function in another Entity from a different entity?

I want to call this function in my breakwall.js..

update: function() {
			if( <= 0){

1 decade ago by Graphikos

Yep, lazer is correct. The issue with your code is the [0]. Your count would return 1 every time because you are pulling one entity (the first one) rather than all of them.

1 decade ago by lazer

If your breakwall entity is already in existence in the level its update() function should already be called continuously every frame while it's alive, so you shouldn't need to trigger it remotely - it'll just keep checking against that if statement on its own. If I'm understanding what you're trying to do correctly, that is.

1 decade ago by TrexKikBut

Thank you! Got it working!
Page 1 of 1
« first « previous next › last »