Jump to content

How to create an area of collision?


narukaioh
 Share

Recommended Posts

Hi guys!

 

I'm trying to create artificial intelligence of the enemies of my game, and I'm finding it difficult to create a method that makes the enemy feel the presence of the player to follow and attack him as well. 
 
I tried to group a geometric shape of a circle and a sprite both positioned at the same coordinates, however checked the documentation I can not use this form. It needs to be a group and / or a sprite.
 
What can I do?
 
The attached image shows the idea of what I do

post-3937-0-19667200-1412994361_thumb.jp

Link to comment
Share on other sites

One idea is to check the distance between the enemy sprite and the player using  Phaser.Math.distance

enemies.forEachAlive(function(enemy) {                            if(!enemy.inCamera)return;  //go to the next enemy if the current selected is out of the camera                          if (game.math.distance(enemy.x, enemy.y , player.x , player, y )  < 50 ) {                              //the enemy is near the player                            //do things              }  }, this);

tweak the "50" value in the if check to find the desired distance.

 

Be sure to check Phaser.Math . You may find something more suited for your needs.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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