WitheringTreant Posted June 15, 2014 Share Posted June 15, 2014 Hello! 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 More sharing options...
eguneys Posted June 15, 2014 Share Posted June 15, 2014 Hi,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 More sharing options...
WitheringTreant Posted June 16, 2014 Author Share Posted June 16, 2014 Hey, thanks alot for your reply, eguneys.It was really helpful. I did what you asked, and a few hours of tinkering from thereon, I now have allytanks killing enemytanks. Link to comment Share on other sites More sharing options...
Recommended Posts