Jump to content

How to make a function prototype target other function prototypes?


Recommended Posts





I've been playing around with the source code of the phaser example game called Tanks (http://examples.phaser.io/_site/view_full.html?d=games&f=tanks.js&t=tanks).


However I have not figured a way to make the tanks target and shoot at the other tanks, so I would appreciate help.



There is a line of code for the EnemyTank function to target the player:

bullet.rotation = this.game.physics.arcade.moveToObject(bullet, this.player, 500);


This makes the bullet fired by an EnemyTank function prototype go to towards the player's tank.

I've been trying to figure out a way to modify the target (this.player) in the above line of code so that it would automatically fire at the closest other enemytank function, but I don't seem to get anywhere with it.



The idea behind is to eventually make EnemyTank function prototypes (enemy tanks) fire bullets at nearby AllyTank prototypes (tanks allied with player).




Link to comment
Share on other sites


EnemyTank doesn't know about the other enemy tanks.


This is how you make an EnemyTank and push it to enemies array:

enemies.push(new EnemyTank(i, game, tank, enemyBullets));
EnemyTank gets a reference to player's tank. So it knows where player tank is, and shoots to this.player.
You should give enemies array as an argument to EnemyTank so it knows about the enemies, then you can calculate which one is nearest.
enemies.push(new EnemyTank(i, game, tank, enemyBullets, enemies));
Link to comment
Share on other sites


  • Recently Browsing   0 members

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