Jump to content

forEach Issue


Recommended Posts

I have a question about forEach in phaser 2.0.4.
I am trying to code the AI of my enemies and want them to just accelerate towards the player as my starting point for the way the game will work.
I can get a single enemy to do it from within the group ive created for my enemies but that is not much use obviously.
I looked in the source and the examples and followed what was said there. I got a few undefined errors but they were down to how i was trying to set things up. I've since resolved that but landed in a weird buggy place.
Now i say buggy, this could just be me not understanding it but it seems a bit strange nonetheless.
My group was originally created with the setAll and createMultiple method. This didnt allow me to use the forEach or other things i like so instead i opted for my preferred method of a for loop.
I've got my enemies in game fine and all works perfectly except for the forEach code im trying. It's giving my a weird error saying:
Uncaught TypeError: Object -2.514152677177211 has no method 'apply' phaser.js:17431
Now that points to this function in phaser:
Phaser.Group.prototype.forEach = function (callback, callbackContext, checkExists) {    if (typeof checkExists === 'undefined') { checkExists = false; }    var args = Array.prototype.splice.call(arguments, 3);    args.unshift(null);    for (var i = 0, len = this.children.length; i < len; i++)    {        if (!checkExists || (checkExists && this.children[i].exists))        {            args[0] = this.children[i];            callback.apply(callbackContext, args); // this is the error line        }    }};

The function and groups causing this are like so:


enemies = this.add.group();for(var j = 0; j < enemyRound; j++){enemy = this.add.sprite(this.world.randomX, this.world.randomY, 'enemy1');enemy.anchor.setTo(0.5,0.5);this.physics.arcade.enable(enemy);enemies.callAll('animations.add','animations','enemywalk',[0,1,2,3,4,5,6,7], 12, true);                enemies.add(enemy);}

and the forEach:

enemies.forEach(this.physics.arcade.moveToObject(enemy, player, 80, 120));

I've also tried 

enemies.forEach(this.physics.arcade.moveToObject(enemies, player, 80, 120));

And ive tried the accelerateToObject too.


Link to comment
Share on other sites

First point, should you not have the line:

enemies.callAll('animations.add','animations','enemywalk',[0,1,2,3,4,5,6,7], 12, true);

outside the for loop, so it only happens once all the enemies are created, instead of once per enemy?



The forEach function is expecting a callback function, a context for the callback, and a bool to see if the object is flagged as existing.

What you have is calling the moveToObject function, and passing the result to the forEach as the callback.


You need something more like this:

enemies.forEach(function(enemy){    this.physics.arcade.moveToObject(enemy, player, 80, 120));}, this);

This may fail on 'player', because I don't know where you defined it.

Link to comment
Share on other sites

The animation may be the cause of my lagged start up when I'm loading the game, I'll move that pronto. Thanks for that answer too, I tried to do a forEach function but it just didn't seem to show any result, no error or nothing. I'll take a try at that function and see if it helps.

Thank you :)

Link to comment
Share on other sites

enemies.forEach(this.physics.arcade.moveToObject(enemies, player, 80, 120));

This syntax is most definitely wrong - what this will do is return the result of this.physics.arcade.moveToObject (which happens to be the angle in radians, hence the error "Object -2.514152677177211 has no method 'apply'" which is telling you that the number -2.514152677177211 returned from the function has no .apply method) to the forEach function, when the forEach function expects the function itself, and will pass that function the individual enemy in its arguments for you to do with as you wish.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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