nicwins Posted March 18, 2017 Share Posted March 18, 2017 I want my enemy to shoot every 2600ms varying 30 degrees in a clockwise rotation. I can get the timing fine with: this.shootingTimer.loop(2600, this.shoot, this); this.shootingTimer.start(); BUT I cant get the 30 degrees going. Is there a simple loop method I could use to do this? I am having trouble... I'm sure someone on here could figure it out pretty quickly: this.game.physics.arcade.velocityFromAngle(90, -100, newElement.body.velocity); //If you see above, I want the 90 to go 0, 30, 60, 90, 120, 150, 180 sequentially every 2 and a half seconds or so.. HELP! Link to comment Share on other sites More sharing options...
nicwins Posted March 18, 2017 Author Share Posted March 18, 2017 I thought to put the numbers into an array and cycle through them somehoiw, but due to lack of programming experience none of my attempts worked. When I did a loop, it just whipped through them in a fraction of a second, the loop worked but didn't coincide with the timer... This should be an easy problem to solve I just can't think of it in this moment. Link to comment Share on other sites More sharing options...
squilibob Posted March 18, 2017 Share Posted March 18, 2017 First you want to calculate each cycle: this.game.time.totalElapsedSeconds() % shootingdelay Then you want to calculate only 7 cycles: cycle % 7 Then you want to multiply the cycle numbers (0-6) by 30 to get your angle. Maybe something like this? const shootingdelay = 2600 this.shootingTimer.loop(shootingdelay, this.calculateshoot, this); this.shootingTimer.start(); calculateshoot: function() { shootingthing.angle = ((this.game.time.totalElapsedSeconds() % shootingdelay) % 7) * 30; this.shoot; } Link to comment Share on other sites More sharing options...
nicwins Posted March 18, 2017 Author Share Posted March 18, 2017 I will try that shortly, is the % modulus? because that is sounding kind of complex, I didn't think I would be needing the modulus for this.. Cant I just do something like: var angleArray = [ 0, 30, 60, 90, 120, 150, 180 ]; if(angleArray.indexOf == 0) { var n = angleArray.indexOf; this.game.physics.arcade.velocityFromAngle(angleArray[n], -100, newElement.body.velocity); n++; } Thats sort of what I was thinking of im not sure if indexOf works like that, but you get what I'm saying... I will try your method though, next chance I get. Thanks. Link to comment Share on other sites More sharing options...
squilibob Posted March 18, 2017 Share Posted March 18, 2017 3 minutes ago, nicwins said: I will try that shortly, is the % modulus? because that is sounding kind of complex, I didn't think I would be needing the modulus for this.. Cant I just do something like: var angleArray = [ 0, 30, 60, 90, 120, 150, 180 ]; if(angleArray.indexOf == 0) { var n = angleArray.indexOf; this.game.physics.arcade.velocityFromAngle(angleArray[n], -100, newElement.body.velocity); n++; } Thats sort of what I was thinking of im not sure if indexOf works like that, but you get what I'm saying... I will try your method though, next chance I get. Thanks. Since all your angles are exactly 30 degrees apart, you should not need the array. You can just have a number from 0-6 and multiply by 30. If you wanted to do it your way just set an integer instead of an array: this.n = 0; this.game.physics.arcade.velocityFromAngle(this.n % 7 * 30, -100, newElement.body.velocity); this.n++; Link to comment Share on other sites More sharing options...
nicwins Posted March 18, 2017 Author Share Posted March 18, 2017 ah huh, I see what you are saying! thanks for your help, i'll let you know how it goes Link to comment Share on other sites More sharing options...
nicwins Posted March 18, 2017 Author Share Posted March 18, 2017 but what if I decide I want to vary the degrees slightly? I'm going to have to pass an array I think Link to comment Share on other sites More sharing options...
nicwins Posted March 18, 2017 Author Share Posted March 18, 2017 I'm going to try and load the angles from the enemyData, that way each enemy object can have its own shooting angles. I'm not sure how I will do thiis, but I will try.. Link to comment Share on other sites More sharing options...
nicwins Posted March 19, 2017 Author Share Posted March 19, 2017 OK, this is what I put into the shoot function: RoboticdogGame.OrangeMonstars.prototype.shoot = function(data) { //look for a dead element var newElement = this.bulletsGroup.getFirstDead(); console.log(data); The console log reads: Array [ 30, 60, 90, 180 ] How can I get that array of numbers being passed to be the angles at which the monsters shoot??? Link to comment Share on other sites More sharing options...
nicwins Posted March 19, 2017 Author Share Posted March 19, 2017 So I got: //look for a dead element var newElement = this.bulletsGroup.getFirstDead(); var anglesArray = data; var anglesLen = anglesArray.length; Now all I need is to cycle through the array one at a time to put into X: this.game.physics.arcade.velocityFromAngle(X, -100, newElement.body.velocity); Link to comment Share on other sites More sharing options...
squilibob Posted March 19, 2017 Share Posted March 19, 2017 You need to get the cycles from a timer. You can use a timer you have created or the game's global timer. What cycle you are on is determined by the timer divided evenly by the shooting delay. so you need a timer like: this.game.time.totalElapsedSeconds() your shooting delay: 2600 the maximum number of cycles: anglesArray.length I can't do any testing for you without all your code but it would be something like this: this.game.physics.arcade.velocityFromAngle((this.game.time.totalElapsedSeconds() % shootingdelay) % anglesLen, -100, newElement.body.velocity); Link to comment Share on other sites More sharing options...
nicwins Posted March 19, 2017 Author Share Posted March 19, 2017 I cant seem to pass: var shootingDelay = 2600; this.shootingTimer.loop(shootingDelay, this.shoot, this, (data.angles, shootingDelay)); into the function: RoboticdogGame.OrangeMonstars.prototype.shoot = function(data, shootingDelay) { //look for a dead element var newElement = this.bulletsGroup.getFirstDead(); var anglesArray = data; var anglesLen = anglesArray.length; Is that a syntax thing?? I will hard code it in for now though Link to comment Share on other sites More sharing options...
squilibob Posted March 19, 2017 Share Posted March 19, 2017 this.shootingTimer.loop(shootingDelay, this.shoot, this, (data.angles, shootingDelay)); I think should just be this.shootingTimer.loop(shootingDelay, this.shoot, this, data.angles, shootingDelay); Link to comment Share on other sites More sharing options...
nicwins Posted March 19, 2017 Author Share Posted March 19, 2017 What about the function?? RoboticdogGame.OrangeMonstars.prototype.shoot = function(x,y) { I tried x,y ... didn't take Link to comment Share on other sites More sharing options...
nicwins Posted March 19, 2017 Author Share Posted March 19, 2017 Sorry! No that worked thanks! I'll try what you said now.. Link to comment Share on other sites More sharing options...
nicwins Posted March 19, 2017 Author Share Posted March 19, 2017 Ok, that sort of worked! But it doesn't utilize the anglesArray! It just divides the length into the timer amount Link to comment Share on other sites More sharing options...
squilibob Posted March 19, 2017 Share Posted March 19, 2017 like this? this.game.physics.arcade.velocityFromAngle( anglesArray[(this.game.time.totalElapsedSeconds() % shootingdelay) % anglesLen], -100, newElement.body.velocity); Link to comment Share on other sites More sharing options...
nicwins Posted March 19, 2017 Author Share Posted March 19, 2017 i'll check Link to comment Share on other sites More sharing options...
nicwins Posted March 19, 2017 Author Share Posted March 19, 2017 hmm well, there was no velocity!?? Link to comment Share on other sites More sharing options...
nicwins Posted March 19, 2017 Author Share Posted March 19, 2017 There's something wrong with the math there: this.game.physics.arcade.velocityFromAngle(anglesArray[(this.game.time.totalElapsedSeconds() % shootingDelay) % anglesLen], 100, newElement.body.velocity); I put in: this.game.physics.arcade.velocityFromAngle(90, 100, newElement.body.velocity); And it shoots Link to comment Share on other sites More sharing options...
nicwins Posted March 19, 2017 Author Share Posted March 19, 2017 I'm not sure about using the game time, because what if I get the enemies to respawn? Link to comment Share on other sites More sharing options...
nicwins Posted March 19, 2017 Author Share Posted March 19, 2017 I gotta go now... will continue this later. thanks SQULI! Link to comment Share on other sites More sharing options...
Recommended Posts