Jump to content

Custom Object is Undefined


igkman
 Share

Recommended Posts

I have a group of Enemy(name of my object) objects that I'm trying to call a certain function from the Enemy class. When I try to do so, nothing would happen. So I wrote in an alert to see if I could access one of the variables from the Enemy class and it told me that the variable was undefined. The code is in the createEnemy class of the Game class. I'm not sure if the Enemy class would be needed or not so I added it here just to be safe.

Game class: 

Game = function(game) {}

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

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

var entranceTimer;
var G1 = [];
var cnt = 0;

  Game.prototype.preload = function() {

	game.load.image('galaga', './assets/images/galaga.png');
	game.load.image('bullet', './assets/images/bullet.png');
	game.load.spritesheet('enemy1', './assets/images/enemy1.png',30,20);
	game.load.spritesheet('enemy2', './assets/images/enemy2.png',30,20);
	game.load.spritesheet('enemy3', './assets/images/enemy3.png',36,32);
	game.load.spritesheet('enemy3Hit', './assets/images/enemy3Hit.png',36,20);
	game.load.image('explosion', './assets/images/explosion.png');
	game.load.spritesheet('pixel','./assets/images/dot.png');
	game.load.audio('pewpew', './assets/sounds/pewpew.wav');

  }

  Game.prototype.create = function() {

	game.physics.startSystem(Phaser.Physics.ARCADE);

	enemies = game.add.group();
	enemies.enableBody = true;

	// locations for enemies
	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
	//group 1
	for (var i = 0; i < 8; i = i+2) {	

		if ( i == 0 || i == 1 || i == 2 || i == 3) {
		enemies.create(game.add.enemy(game.width/1.33,0,group1PixelLocations.x[i],group1PixelLocations.y[i],1));
		//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));
	}
		else {
		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],0));
		}
	}

	this.createEnemy();
	
}

Game.prototype.update = function() {

}



Game.prototype.createEnemy = function() {

	// supposed to store an Enemy object into the G1 array from the
	//enemies group
	enemies.forEach(function(en) {
		// gets xPix var from Enemy class
		alert(en.xPix);
		G1.push(en);

	});
	
	
}

Game.prototype.shootFunction = function() {
	bullet.fire()
}

Enemy Class: the function that I was trying to call originally was group1Path, but I couldn't due to the Enemy object being 'undefined'

 

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

	// these two variables are undefined
	//alert(xPix + " " + yPix);
	this.xPix = xPix;
	this.yPix = yPix;
	this.enemyNum = enemyNum;

	this.create();
	
}

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


Enemy.prototype.preload = function() {

}

Enemy.prototype.create = function() {

		var enemySprites = ['enemy1','enemy2','enemy3'];
		// enemy sprite and its animation
		this.enemy = game.add.sprite(this.x,this.y,enemySprites[this.enemyNum]);
		this.enemy.anchor.set = 0.5;
		this.enemy.animations.add(enemySprites[this.enemyNum],null,10,true);
		this.enemy.animations.play(enemySprites[this.enemyNum]);

		//This will be tracked by the enemy sprite
		//will be set to invisible
		this.pixel = game.add.sprite(this.xPix,this.yPix,'pixel');
		this.pixel.visible = false;

		game.physics.arcade.enable(this.enemy);
		game.physics.arcade.enable(this.pixel);	
		


		this.sideTween();

}

var completed = false;
Enemy.prototype.update = function() { 

		if (!this.exists)	return;

		// when group1Path is completed, var compelete = true
		// then enemy will follow the pixel 
		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() {

//function

}


}

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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