Jump to content

Check for closest member of group


Recommended Posts

Hello every one. This is my first post after playing around with Phaser for a while. I dont have much free time, so my progress is slow, but I manage to get the hang of the basics. 


For my first project I want to create a board of "grass" tiles (32x32 px each) that fill the screen, and then add a group of "herbivores" that will roam around and eat the grass.


I know it sounds boring, but this is just for the beginning stage, later on I will add also "carnivores" (player) to eat said herbivores. 


Any way, I played around with the examples and managed to fill the screen with grass tiles, and to add 20 herbivores that appear at random (x,y) snapping to 32px intervals, so they will always be in the center of a tile.


My first question is, what is the difference between an array and a group. 


I created my grass tiles the same way the gems are created in the "Gemmatch" game from the examples:


    field = game.add.group();

    for (var i = 0; i < BOARD_COLS; i++)
        for (var j = 0; j < (BOARD_ROWS; j++)
            var grassone = field.create(i * 32, j * 32, 'grassone');
            grassone.name = 'grassone' + i.toString() + 'x' + j.toString();
            grassone.inputEnabled = true;


But for the herbivores I created them using the same method the enemy tanks are created in the Tanks game from the examples:


createHerbivore = function (index, game, herbivore) {
    var x = (Phaser.Math.snapTo(game.world.randomX,32) + 16);
    var y = (Phaser.Math.snapTo(game.world.randomY,32) + 16);
    this.game = game;
    this.herbivore = game.add.sprite(x, y, 'herbivore');
    this.herbivore.animations.play('idle', 10, true);
    this.herbivore.angle = game.rnd.angle();
    this.herbivore.name = index.toString();
    this.health = 1;
    this.alive = true;
    this.herbivore.inputEnabled = true;
    game.physics.enable(this.herbivore, Phaser.Physics.ARCADE);
    this.herbivore.events.onInputDown.add(killHerbivore, this); //yes, they die when you click on them
With this inside the function Create ()
herbivores = [];
    herbivoresTotal = 20;
    herbivoresAlive = 20
    for (var i = 0; i < herbivoresTotal; i++)
        herbivores.push(new createHerbivore(i, game, this));
So my question is what is the technical difference between groups and arrays? 
My second question is, how do I make my herbivores find the closest member of their group and move towards it. And how can I make them search the closes patch of grass (I'll make them kill the grass after a few seconds so they can move on)
I will work out the details of how close to stay to the group while still searching for unkilled grass, and how often grass will regrow, later on; but for now I just want to know how I can make each member of the pack be aware of the closest member and the closest grass.
Thanks everyone in advance!
Link to comment
Share on other sites

Both a group and an array can work as a way to hold references to a bunch of sprites. An array is purely JavaScript and so you have available it's built in methods to select and manipulate its contents.

Groups are a Phaser class and similarly can be used to manipulate and select the contents but with many added features. For example a group has its own coordinates so by moving the group you move all of the sprites. Or if you need to access a sprite that has its alive property set to false you,can use group.getFirstDead();

You could do much of what a group does using an array but as you're using a framework to make life easier often a group is the best solution for holding sprites. it is possible that you may have an array and a group both reference the same sprites if you identify a need to do so.

There are some excellent examples on this site that may help with your other question. There aren't any directly comparable but may well work as good starting point. Maybe the flocking example in the missiles section.


Link to comment
Share on other sites

Thanks for the info!


I suppose I should make the herbivores into a group as well, instead of an array. The creating a group seems cleaner code too. 


I also took a peek at those examples and the missile flocking seems to be right on the money with what I want (which also works with groups). I will play around with these and I will post again with new questions =).


Perhaps once Im done with the basics I'll post it as an example, maybe even make it somewhat of a tutorial, from a newbie for the newbies. 

Link to comment
Share on other sites


  • Recently Browsing   0 members

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