Jump to content

Control firing rate of a gun.


j0hnskot
 Share

Recommended Posts

Hello there! Im trying to make a shooter game where i shoot enemies and the enemies shoot back.

To make the enemy shoot i made an invisible line following the player and when the enemy and the invisible line overlaps the enemy shoots.

But the problem is that he shoots many times since the enemy overlaps many times its frame! Is there a way to control the rate of fire? Or is there a different approach? 

 

 

Thank you in advance!

Link to comment
Share on other sites

Something like this maybe?

enemy.fireRate = 250; // firing rate in msenemy.lastFiredTime = 0;function update() {  // lineIsOverlapping would be the result of your line checking routine  if (lineIsOverlapping && enemy.lastFiredTime + enemy.fireRate < game.time.now) {    enemy.fireAtPlayer();    enemy.lastFiredTime = game.time.now;  }}
Link to comment
Share on other sites

Sounds really good! But since I'm using a group of many enemy sprites , to use something like that i must make a "Enemy" class with a property of fireRate and lastFiredTime. Am i right? Or there is a way to add custom properties to sprites?

Link to comment
Share on other sites

First of all , thanks for this knowledge! 

I was ready to write a wall of text because i tried to set the properties on the enemy sprite but when i did the check you suggested it didn't work because it said that the properties was unidentified. Turned out to be a strange (at least for me) problem.

 

when i used the "  game.physics.arcade.overlap(enemies, invisible_line, enemyShoot, null, this); "

to pass the objects on my function, the order was wrong! It passed first the invisible_line and then the enemies! 

Since i don't think opening a new thread for this thing is right, do you have any idea why this strange thing happened?

 

Thank you , again!

Link to comment
Share on other sites

I think I've gotten my logic/maths a little muddled in my example which may not be helping, but here is a fuller example. Note the change to the comparison in the update function.

var enemiesGroup = game.add.group();var enemy; // temp variable so we can set some properties on each enemyfor (var e = 0; e < 10; e++) { // create 10 enemies  enemy = game.add.sprite(0, 0, 'enemy', 0, enemiesGroup);  enemy.fireRate = 250; // firing rate in ms  enemy.lastFiredTime = 0; // last fired time, set to 0 as we wish to make a numeric comparison}function update() {  // lineIsOverlapping would be the result of your line checking routine  if (lineIsOverlapping && enemy.lastFiredTime + enemy.fireRate < game.time.now) {    enemy.fireAtPlayer();    enemy.lastFiredTime = game.time.now;  }}
Link to comment
Share on other sites

Perfect, thanks!

 

Now for my other question. Is there a reason that the  game.physics.arcade.overlap(enemies, invisible_line, enemyShoot, null, this);

passes first the "invisible_line" and then the "enemies? Shouldn't be the other way around? That's pretty confusing!

Ofcourse it's not that a big problem, i just swapped places on my function's arguements, but still, i think it's pretty strange.

Link to comment
Share on other sites

You may need to check which version of Phaser you're using, as I think previously in the event of a sprite-to-group comparison, the individual sprite is always returned as the first item, and the item from the group is returned as the second. In the latest version (2.0.4) they should be returned in the order specified according to the docs.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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