piobug

Load different enemies per level (js constructor question)

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;

 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.