Jump to content

moveToObject help!


MathiasaurusRex
 Share

Recommended Posts

Hey there!

I'm working on a simple top down shooter where the enemies will move toward the player. You can view the current code at my website.

 

Currently the red blocks move toward the cursor using the following snippet (modified from the moveToObject tutorial) which moves the red blocks toward the pointer:

 

if (gameStart === true)    {        //  First is the callback        //  Second is the context in which the callback runs, in this case game.physics        //  Third is the parameter the callback expects - it is always sent the Group child as the first parameter        enemies.forEach(game.physics.moveToPointer, game.physics, false, 30);    }    else    {        enemies.setAll('body.velocity.x', 0);        enemies.setAll('body.velocity.y', 0);    }

I've tried a few things but essentially what I want the enemies to do is moveToObject and in this case I want the object to be the player.

enemies.forEach(game.physics.moveToObject(player), game.physics, false, 30);

I've also gone through the tank code and was couldn't quite get what I wanted to work.

 

Would appreciate any help on this!

 

Link to comment
Share on other sites

You can have fun playing around with this (I just did):

 

     enemies.forEach( function(enemie) {
        this.accelerateToObject(enemie, player, 600, 250, 250);
      }, game.physics);
 
 
But, you have to declare
"var player = null;" 
where you declare your other global variables.
 
if you increase the first number, you increase the ability to change direction for the enemy (it will follow better by accelerating harder).
The next too nunbers limit the x and the y speed.  (See phaser doc for details)
 
It might not be exaclty what you are looking for, but I liked the effect :) (looks a bit like a dog-fight in space) Anyway I hope it helps you.
 
If you purely want to move the enemy closer to the player (without acceleration and stuff, you could just increase enemie.body.x if it's smaller then player.body.x, decrease if bigger, same for y) all in the foreach function (so that it's run for all the enemies available.)
 
 
Also your enemies.anchor.setTo does not work (you have to do that for each enemie I think, using foreach - there is no anchor on a group as far as i know)
 
Use F12 (in chrome) to open the Console to display errors (that's how I found the wrong achor, and how i figure out what else I do wrong in my code.)
Link to comment
Share on other sites

That accelerateToObject snippet you posted was pretty cool, thanks for sharing!

 

It ultimately got me to the functionality that I was seeking:
 

enemies.forEach(            function(singleEnemy) {                this.moveToObject(singleEnemy, player, 20);            }, game.physics);

I'm not sure if this is the most elegant solution, but think of shambling zombies.

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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