IHateThisMap Posted November 13, 2015 Share Posted November 13, 2015 I'm trying to pass a string literal as an argument and use it in groups.createMultiple function but when I do i get the error "TypeError: this._frames[a[d]] is undefined". Why can I not do this and what would be a better way. Thanks for the help. setupEnemies: function() { this.enemyBlack01Pool = this.createEnemyGroup('enemyBlack01'); this.enemyBlack02Pool = this.createEnemyGroup('enemyBlack02'); this.enemyBlack03Pool = this.createEnemyGroup('enemyBlack03'); this.enemyBlack04Pool = this.createEnemyGroup('enemyBlack04'); this.enemyBlue01Pool = this.createEnemyGroup('enemyBlue01'); this.enemyBlue02Pool = this.createEnemyGroup('enemyBlue02'); this.enemyBlue03Pool = this.createEnemyGroup('enemyBlue03'); this.enemyBlue04Pool = this.createEnemyGroup('enemyBlue04'); this.enemyGreen01Pool = this.createEnemyGroup('enemyGreen01'); this.enemyGreen02Pool = this.createEnemyGroup('enemyGreen02'); this.enemyGreen03Pool = this.createEnemyGroup('enemyGreen03'); this.enemyGreen04Pool = this.createEnemyGroup('enemyGreen04'); this.enemyLightBlue01Pool = this.createEnemyGroup('enemyLightBlue01'); this.enemyLightBlue02Pool = this.createEnemyGroup('enemyLightBlue02'); this.enemyLightBlue03Pool = this.createEnemyGroup('enemyLightBlue03'); this.enemyLightBlue04Pool = this.createEnemyGroup('enemyLightBlue04'); this.enemyOrange01Pool = this.createEnemyGroup('enemyOrange01'); this.enemyRed01Pool = this.createEnemyGroup('enemyRed01'); this.enemyRed02Pool = this.createEnemyGroup('enemyRed02'); this.enemyWhite01Pool = this.createEnemyGroup('enemyWhite01'); this.enemyWhite02Pool = this.createEnemyGroup('enemyWhite02'); this.enemyWhite03Pool = this.createEnemyGroup('enemyWhite03'); }, createEnemyGroup: function(sprtie) { var group = this.add.group(); group.enableBody = true; group.physicsBodyType = Phaser.Physics.ARCADE; group.createMultiple(10, sprtie); group.setAll('anchor.x', 0.5); group.setAll('anchor.y', 0.5); group.forEach(function(enemy) { enemy.animations.add('fly', [0], 20, true); enemy.animations.add('hit', [0, 1, 0, 1, 0, 1, 0], 20, false); enemy.events.onAnimationComplete.add(function(e) { e.play('fly'); }, this); }); return group; } Link to comment Share on other sites More sharing options...
rich Posted November 13, 2015 Share Posted November 13, 2015 I expect the error is the forEach loop, nothing else - without looking too much into it my guess is that the JavaScript scope/context is wrong within in. Link to comment Share on other sites More sharing options...
jmp909 Posted November 13, 2015 Share Posted November 13, 2015 should workhttp://phaser.io/sandbox/UogNmYmc/play so yes maybe you have a scope issue somewhere. try and determine which line is causing the error by commenting/uncommenting portions bit by bit Link to comment Share on other sites More sharing options...
jfhs Posted November 14, 2015 Share Posted November 14, 2015 Hi IHateThisMap! You are passing no "this" context to .forEach(), but use "this" in the callback. Does it work if you change it as follows? Look at the last line here: group.forEach(function(enemy) { enemy.animations.add('fly', [0], 20, true); enemy.animations.add('hit', [0, 1, 0, 1, 0, 1, 0], 20, false); enemy.events.onAnimationComplete.add(function(e) { e.play('fly'); }, this); }, this);In JavaScript, a function defined inside another function doesn't automatically have the parent function's "this" reference. If you want to force a function to have a context, you have to .bind() it to a context like so:function () { /* ... */ }.bind(context) Link to comment Share on other sites More sharing options...
Recommended Posts