Jump to content

distanceBetween question


Nek0
 Share

Recommended Posts

suppose you have two different lines:

game.physics.arcade.overlap(pl, bat, function(sp, sp2) {sp2.kill()})

and

if (game.physics.arcade.distanceBetween(pl, bat) < 222) {bat.kill()}

the top detects when the player touches a randomly placed bat in a group and kills it, the second detects when the player is in RANGE of a randomly placed bat in a group and kills it. the problem is the second one doesn't work. i'm essentially looking for a way to merge these two so that the bat in line 2 dies the way the one in line 1 does.

this doesn't seem to work either:

if (game.physics.arcade.distanceBetween(pl, bat, function(sp, sp2) < 222)) {sp2.kill()}

 

Link to comment
Share on other sites

  • 2 weeks later...

anyone? the basic question i guess is how do you assign unique functions to individual sprites in a group of 6 without having to assign variables to all 6. for instance, if you made the sprites like this:

for (i = 0; i < 6; i++) {bat.create(Math.random() * 600 + 300, Math.random() * 600 + 300, 'bat')
bat.callAll('animations.add', 'animations', '1', [0, 1], 6, true); bat.callAll('play', null, '1')}

the top line creates 6 randomly placed "bats" in a perimeter while the bottom one adds and plays animations for all. i'm looking for an effect like the bottom line, except one that detects when the player gets within a certain distance of one of the bats and makes the same bat flutter around randomly (legend of zelda style).

Link to comment
Share on other sites

15 hours ago, Nek0 said:

anyone? the basic question i guess is how do you assign unique functions to individual sprites in a group of 6 without having to assign variables to all 6. for instance, if you made the sprites like this:


for (i = 0; i < 6; i++) {bat.create(Math.random() * 600 + 300, Math.random() * 600 + 300, 'bat')
bat.callAll('animations.add', 'animations', '1', [0, 1], 6, true); bat.callAll('play', null, '1')}

the top line creates 6 randomly placed "bats" in a perimeter while the bottom one adds and plays animations for all. i'm looking for an effect like the bottom line, except one that detects when the player gets within a certain distance of one of the bats and makes the same bat flutter around randomly (legend of zelda style).

One way I can think of is iterate through the living members of bat group and for each active member of group check the distance between that member and player. Once the distance is within limit run your flutter code. This is currently one way I can think of there could be a better way if you dig through examples of Phaser v2

Link to comment
Share on other sites

10 hours ago, Horizonicblue said:

One way I can think of is iterate through the living members of bat group and for each active member of group check the distance between that member and player. Once the distance is within limit run your flutter code. This is currently one way I can think of there could be a better way if you dig through examples of Phaser v2

thanks so much for the reply. could i trouble you for an example? i checked for one on the phaser website but couldn't find one for the desired effect.

Link to comment
Share on other sites

Here is a short example I wrote https://phaser.io/sandbox/edit/DuRpdEGQ

This loop with check for distance is used to call the flutter action, try to make it more suitable for your use case

bats.forEachAlive(function(bat){
    if (game.physics.arcade.distanceBetween(player, bat) < 40 && !bat.fluttering) {
        bat.fluttering = true;
        flutter(bat);
    }
});

Hope it helps

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...