Sign in to follow this  
WitheringTreant

How to make a function prototype target other function prototypes?

Recommended Posts

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).

 

 

 

Share this post


Link to post
Share on other sites

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));

Share this post


Link to post
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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.