piotr Posted November 13, 2017 Share Posted November 13, 2017 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 More sharing options...
samme Posted November 13, 2017 Share Posted November 13, 2017 Save it on this.game.levelData instead. Machine-dev 1 Link to comment Share on other sites More sharing options...
piotr Posted November 14, 2017 Author Share Posted November 14, 2017 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 More sharing options...
samme Posted November 14, 2017 Share Posted November 14, 2017 Oh, never mind that, I thought you were having a scope problem. In levelData, use enemy: 'Teacher', … enemy: 'Seeker', piotr and Machine-dev 2 Link to comment Share on other sites More sharing options...
piotr Posted November 15, 2017 Author Share Posted November 15, 2017 Thanks! It works! Link to comment Share on other sites More sharing options...
Recommended Posts