igkman Posted November 24, 2016 Share Posted November 24, 2016 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 More sharing options...
WombatTurkey Posted November 24, 2016 Share Posted November 24, 2016 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? Link to comment Share on other sites More sharing options...
igkman Posted November 24, 2016 Author Share Posted November 24, 2016 How is that? The problem lies within the update function of the Game class. Link to comment Share on other sites More sharing options...
WombatTurkey Posted November 24, 2016 Share Posted November 24, 2016 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) Link to comment Share on other sites More sharing options...
igkman Posted November 24, 2016 Author Share Posted November 24, 2016 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? Link to comment Share on other sites More sharing options...
igkman Posted November 25, 2016 Author Share Posted November 25, 2016 That's what it's suppose to do. I might be misinterpreting the function though Link to comment Share on other sites More sharing options...
Recommended Posts