Jump to content

New to Phaser & JS, probably just a syntax mistake.


IHateThisMap
 Share

Recommended Posts

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

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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