# How to call a custom function of a child in a group

## Recommended Posts

``````Game = function(game) {

this.timer = 0;
this.cycle = 1000;
this.cnt = 0;

}

Phaser.GameObjectFactory.prototype.enemy = function(x,y,xPix,yPix,enemyNum) {

}

Game.prototype = {

create: function() {

enemies.enableBody = true;

// locations for the first group of enemies to fly in
var group1PixelLocations = {
'x' : [game.width/2,game.width/2-30,game.width/2,game.width/2-30,
game.width/2,game.width/2-30,game.width/2,game.width/2-30],
'y' : [(game.height/2)-30,(game.height/2)-30,(game.height/2)-60,(game.height/2)-60,
(game.height/2)-90,(game.height/2)-90,(game.height/2)-120,(game.height/2)-120],
};

//create enemies
for (var i = 0; i < 8; i = i+2) {
//create another enemy object with opposite coordinates
}

},

update: function() {

if (game.time.now > this.timer) {

this.timer = game.time.now + this.cycle;

var en = enemies.getAt(this.cnt);
// this is the function I want each child to perform
en.group1Path();

this.cnt = this.cnt + 1;
}

}

};``````

I'm trying to call a function that I created in the Enemy class(below) with the children in my group. However, the error I get is 'Uncaught TypeError: en.group1Path is not a function' . Which is odd becuase the group consists of Enemy objects.

Enemy class:

``````var Enemy = function(game,x,y,xPix,yPix,enemyNum) {
Phaser.Sprite.call(this,game,x,y,'');

this.xPix = xPix;
this.yPix = yPix;
this.enemyNum = enemyNum;
this.create();

}

Enemy.prototype = Object.create(Phaser.Sprite.prototype);
Enemy.prototype.constructor = Enemy;

var completed = false;

}

Enemy.prototype.create = function() {

//this.group1Path();
}

Enemy.prototype.update = function() {

if (!this.exists)	return;

if (completed)
}

Enemy.prototype.isComplete = function() {
completed = true;
}

//path of the first group
Enemy.prototype.group1Path = function() {

/*This is the function to be called*/

}``````

##### Share on other sites

A lot of unneeded code I think, can u try to skim it down a bit and isolate what's exactly going on / where the error lies?

##### Share on other sites

How is that? The problem lies within the update function of the Game class.

##### Share on other sites

57 minutes ago, igkman said:

How is that? The problem lies within the update function of the Game class.

What is  var en = enemies.getAt(this.cnt);  z

returning? console.log(en)

##### Share on other sites

enemies.getAt(this.cnt) is supposed to return a child of the group "enemies" at the index 'this.cnt'. cnt is an integer set at zero.

does that help?