j0hnskot Posted May 19, 2014 Share Posted May 19, 2014 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 More sharing options...
lewster32 Posted May 19, 2014 Share Posted May 19, 2014 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 More sharing options...
j0hnskot Posted May 19, 2014 Author Share Posted May 19, 2014 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 More sharing options...
lewster32 Posted May 19, 2014 Share Posted May 19, 2014 JavaScript supports fully dynamic objects, you don't need to predefine them. You can simply add properties to any object (including built-in Phaser objects) at any time, so adding these to the sprites directly isn't a problem. Link to comment Share on other sites More sharing options...
lewster32 Posted May 19, 2014 Share Posted May 19, 2014 Though yes, best practice would be to either extend the Phaser.Sprite object, or compose it into an object (or 'class' if you really insist on calling it that) just to keep it clean. Link to comment Share on other sites More sharing options...
j0hnskot Posted May 19, 2014 Author Share Posted May 19, 2014 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 More sharing options...
lewster32 Posted May 19, 2014 Share Posted May 19, 2014 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 More sharing options...
j0hnskot Posted May 19, 2014 Author Share Posted May 19, 2014 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 More sharing options...
lewster32 Posted May 19, 2014 Share Posted May 19, 2014 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 More sharing options...
j0hnskot Posted May 19, 2014 Author Share Posted May 19, 2014 That's explains a lot,thanks! I'm using 2.0.4 but atleast now i know what behaviour to expect! Link to comment Share on other sites More sharing options...
j0hnskot Posted May 20, 2014 Author Share Posted May 20, 2014 What an update! As of 2.0.5 you can now use the 'force' parameter of Group.setAll to assign new properties to all childs! Link to comment Share on other sites More sharing options...
Recommended Posts