Jump to content

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


igkman
 Share

Recommended Posts

Game = function(game) {

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

}

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

	return this.game.add.existing(new Enemy(this.game,x,y,xPix,yPix,enemyNum) );
}

Game.prototype = {

  create: function() {

	game.physics.startSystem(Phaser.Physics.ARCADE);
	enemies = game.add.group();
	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) {	
		enemies.create(game.add.enemy(game.width/1.33,0,group1PixelLocations.x[i],group1PixelLocations.y[i],0));
		//create another enemy object with opposite coordinates
		enemies.create(game.add.enemy(game.width/4,0,group1PixelLocations.x[i+1],group1PixelLocations.y[i+1],1));
	}
	
},

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.preload = function() {

}

Enemy.prototype.create = function() {

		//this.group1Path();	
}


Enemy.prototype.update = function() { 

		if (!this.exists)	return;

		if (completed)
		game.physics.arcade.moveToObject(this.enemy,this.pixel,100);
}





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

//path of the first group
Enemy.prototype.group1Path = function() {
 
/*This is the function to be called*/

}

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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