Jump to content

Control individual members of a group


Recommended Posts

Hello all.  I just discovered Phaser.io and think it is great.  I am a bit stuck, however, on how to manipulate members of a group.  I have a space shooter where I spun up about a dozen enemies within a group.  Now I want to select individual members of the groups to move toward the player in some pattern.  I can't figure out how to get just one member of the group.  I tried something like:

var leader=enemies.getFirstAlive();leader.x-=1;

 but it did not work.  Also, what if I wanted it to be a random enemy that was alive?  Any guidance would be most appreciated.



Link to comment
Share on other sites

use an alert(myStuffhere), instead of console, it's less handy for debugging, but that should do the trick for now


Also, (and again), take a close look at the code structure of this example http://examples.phaser.io/_site/view_full.html?d=games&f=invaders.js&t=invaders


And spot what fundamentally differs from yours .










var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });

function preload() {

game.load.image('bullet', 'assets/games/invaders/bullet.png');


var player;
var aliens;
var bullets;

function create() {


// The scrolling starfield background
starfield = game.add.tileSprite(0, 0, 800, 600, 'starfield');

// Our bullet group
bullets = game.add.group();
bullets.enableBody = true;
bullets.physicsBodyType = Phaser.Physics.ARCADE;
bullets.createMultiple(30, 'bullet');
bullets.setAll('anchor.x', 0.5);
bullets.setAll('anchor.y', 1);
bullets.setAll('outOfBoundsKill', true);
bullets.setAll('checkWorldBounds', true);

// The enemy's bullets
enemyBullets = game.add.group();
enemyBullets.enableBody = true;
enemyBullets.physicsBodyType = Phaser.Physics.ARCADE;
enemyBullets.createMultiple(30, 'enemyBullet');
enemyBullets.setAll('anchor.x', 0.5);
enemyBullets.setAll('anchor.y', 1);
enemyBullets.setAll('outOfBoundsKill', true);
enemyBullets.setAll('checkWorldBounds', true);

// The hero!
player = game.add.sprite(400, 500, 'ship');
player.anchor.setTo(0.5, 0.5);
game.physics.enable(player, Phaser.Physics.ARCADE);

// The baddies!
aliens = game.add.group();
aliens.enableBody = true;
aliens.physicsBodyType = Phaser.Physics.ARCADE;




function update() {

// Scroll the background
starfield.tilePosition.y += 2;

// Reset the player, then check for movement keys
player.body.velocity.setTo(0, 0);

if (cursors.left.isDown)
player.body.velocity.x = -200;
else if (cursors.right.isDown)
player.body.velocity.x = 200;


// Run collision
game.physics.arcade.overlap(bullets, aliens, collisionHandler, null, this);
game.physics.arcade.overlap(enemyBullets, player, enemyHitsPlayer, null, this);





function collisionHandler (bullet, alien) {

// When a bullet hits an alien we kill them both






function enemyHitsPlayer (player,bullet) {




Link to comment
Share on other sites

The alert(console.log(leader)); shows "undefined"


The code for the invaders has the whole group moving - which I can do.  This is what the aliesn do - which I believe impacts the entire group.

function descend() {    aliens.y += 10;}

I am wondering how to get the first alien to move some way without moving the others in the group.

Link to comment
Share on other sites

Okay, now it says "[object Object]" in the alert. 


This is how I create the members of the group:

//enemies	enemies=game.add.group();  	enemies.enableBody = true;    enemies.physicsBodyType = Phaser.Physics.ARCADE;   // enemies.createMultiple(30, 'galaga');	for (var i = 0; i < 7; i++)    {         	yellowJacket = enemies.create(game.world.width-40, 75 * i, 'galaga', 'yellowJacket');		yellowJacket.body.gravity.x = horizGravity*-1;    }		for (var i = 0; i < 6; i++)    {         	yellowJacket = enemies.create(game.world.width-90, (75 * i)+100, 'galaga', 'yellowJacket');		yellowJacket.body.gravity.x = horizGravity*-1;    }		for (var i = 0; i < 5; i++)    {         	yellowJacket = enemies.create(game.world.width-140, (75 * i)+125, 'galaga', 'yellowJacket');		yellowJacket.body.gravity.x = horizGravity*-1;    }	    enemies.setAll('anchor.x', 0.5);    enemies.setAll('anchor.y', 1);
Link to comment
Share on other sites

horizGravity currently = 0 so that would not do anything ... it is the same as * -1.  I shut off the gravity in favor of trying to move members of the group.  I know that on creation I could do something like this:

body.velocity.x = game.rnd.integerInRange(-200, 200);

to set the individual members in motion.  Now I am trying to call them individually after the creation to move them.  I wanted to start with firstAlive ... but I don't know why it does not work.


EDIT: I am trying to do this in the update loop by calling the function

function leaderAttack(){var leader=enemies.getFirstAlive();leader.body.velocity.x-=10;}

The getFirstAlive() seems to be returning an object.  Is the problem that I am constantly calling it before I attempt the move?


EDIT 2:  This works to select the first enemy and move it:


Don't know why getFirstAlive won't do the same thing?


EDIT 3: getRandom also works - will eventually move them all since it is in a loop that gets called very frequently (could slow it down with a timer) but it works.

Link to comment
Share on other sites

@Heppell08 - if I understand your post correctly that would be fine for iterating every member of the group to a common action (setAll might work too).  However, I just want to work with individual members of the group without regard to disturbing t he rest of the group.   That is why enemies.getAt(1).body.velocity.x-=1; works to a degree by allowing me to select any one member of the group to move.  That was the thrust of the original post.  However, I still don't understand why group.getFirstAlive().body.velocity.x-=1; does not do the same thing ...

Link to comment
Share on other sites


  • Recently Browsing   0 members

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