index.toString() problem

Hi there, I am trying to spawn several enemies using a loop method however I keep getting an error called Cannot read property ‘toString’ of undefined.

Here is where I have called my spawn of enemies:

enemyPlayer = function (index, game, player, x, y, bullets) {

var x = Phaser.Math.RND.between(0, 800);
var y = Phaser.Math.RND.between(0, 600);

  this.game = game;
  this.tank.name = index.toString();
  this.health = 3;
  this.player = player;
  this.bullets = bullets;
  this.fireRate = 1000;
  this.nextFire = 0;
  this.alive = true;

  this.tank = this.physics.add.sprite(x, y, 'enemy', 'tank1').setOrigin(0.5, 0.5);
  this.turret = this.physics.add.sprite(x, y, 'enemy', 'turret').setOrigin(0.3, 0.5);
  //this.physics.world.enable(this.tank);

  this.tank.body.immovable = false;
  this.turret.body.immovable = false;
  this.tank.body.collideWorldBounds = true;
  this.turret.body.collideWorldBounds = true;
  this.tank.body.bounce.setTo(1, 1);
  this.turret.body.bounce.setTo(1, 1);

  this.physics.velocityFromRotation(this.tank.rotation, 100, this.tank.body.velocity);
  this.physics.velocityFromRotation(this.tank.rotation, 100, this.turret.body.velocity);

};

And this is in my create function:

enemies = [];

        enemiesTotal = 20;
        enemiesAlive = 20;

        for (var i = 0; i < enemiesTotal; i++)
        {
            enemies.push(enemyPlayer.call(i, game, tank, 0, 0, this));
        }

Hey Faizan, it’s telling you that your parameter, index is undefined (Likely null). console.log your first argument, index to see.

Hey Jake, it’s telling me that index is undefined.

That means when you create your enemyPlayer, index was never assigned a value. If you only have one enemy tank, then I would simply change

this.tank.name = index.toString();

to something like

this.tank.name = "myTank";

Sorry I don’t quite understand, if I want to create an array that will create my enemies how would I call the index?

E.g. how will I call the 20 enemies here:

enemies = [];

        enemiesTotal = 20;
        enemiesAlive = 20;

        for (var i = 0; i < enemiesTotal; i++)
        {
            enemies.push(enemyPlayer.call(i, game, tank, 0, 0, this));
        }
function enemyPlayer (
  index,
  game,
  player,
  x,
  y,
  bullets,
  scene
) {

  var x = Phaser.Math.RND.between(0, 800);
  var y = Phaser.Math.RND.between(0, 600);

  this.game = game;
  this.tank.name = index.toString();
  this.health = 3;
  this.player = player;
  this.bullets = bullets;
  this.fireRate = 1000;
  this.nextFire = 0;
  this.alive = true;

  this.tank = scene.physics.add.sprite(x, y, 'enemy', 'tank1').setOrigin(0.5, 0.5);
  this.turret = scene.physics.add.sprite(x, y, 'enemy', 'turret').setOrigin(0.3, 0.5);
  //scene.physics.world.enable(this.tank);

  this.tank.body.immovable = false;
  this.turret.body.immovable = false;
  this.tank.body.collideWorldBounds = true;
  this.turret.body.collideWorldBounds = true;
  this.tank.body.bounce.setTo(1, 1);
  this.turret.body.bounce.setTo(1, 1);

  scene.physics.velocityFromRotation(this.tank.rotation, 100, this.tank.body.velocity);
  scene.physics.velocityFromRotation(this.tank.rotation, 100, this.turret.body.velocity);
};

// …

enemies.push(
  new enemyPlayer(
    i,
    game,
    tank,
    0,
    0,
    bullets,
    this
  )
);