Jump to content

Load different enemies per level (js constructor question)


piotr
 Share

Recommended Posts

Hi all,  I have the game object "levelData" in "menu.js", that decides which assets will be loaded per level. In "play.js" I manage to load a different map per level, but I can't figure out how to create a different enemy per level using the new constructor. Thanks

 

var menuState = {
  init: function() {
      this.levelData = {
        1: { 
          enemy: 'teacher',
          map: 'teacher',
          timeScore: ''
        },
        2: {
          enemy: 'seeker',
          map: 'base',
          timeScore: ''
        }
      };
},
//other stuff...
var playState = {

    init: function(levelData) {
        this.levelData = levelData;
    },
 
   create: function() {
       //this works
       this.map = game.add.tilemap(this.levelData.map);
       
       //this ofc doesn't
       this.enemy = new Enemy[this.levelData.enemy] (game, this.player);
       this.add.existing(this.enemy);

   },
};
var Enemy = {};

  Enemy.Teacher = function (game,target) {
     //relevant code
  };
  Enemy.Teacher.prototype = Object.create(Phaser.Sprite.prototype);
  Enemy.Teacher.prototype.constructor = Enemy.Teacher;

  Enemy.Seeker = function (game,target) {
  	  //relevant code
  };
  Enemy.Seeker.prototype = Object.create(Phaser.Sprite.prototype);
  Enemy.Seeker.prototype.constructor = Enemy.Seeker;

 

Link to comment
Share on other sites

Do you mean like this?

init: function(levelData) {
    this.game.levelData = levelData;
},  

create: function() {
  this.enemy = new Enemy[this.game.levelData.enemy] (game, this.player);
  this.add.existing(this.enemy);
}

I get a " Enemy[this.game.levelData.enemy] is not a constructor" error message

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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