# shooting at angles problem? help pls

## Recommended Posts

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:

//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!

##### Share on other sites

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.

##### Share on other sites

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;

}``````

##### Share on other sites

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;

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.

##### Share on other sites

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;

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

##### Share on other sites

ah huh, I see what you are saying! thanks for your help, i'll let you know how it goes

##### Share on other sites

but what if I decide I want to vary the degrees slightly? I'm going to have to pass an array I think

##### Share on other sites

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

##### Share on other sites

OK, this is what I put into the shoot function:

RoboticdogGame.OrangeMonstars.prototype.shoot = function(data) {

console.log(data);

Array [ 30, 60, 90, 180 ]

How can I get that array of numbers being passed to be the angles at which the monsters shoot???

##### Share on other sites

So I got:

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:

##### Share on other sites

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

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

##### Share on other sites

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

var anglesArray = data;
var anglesLen = anglesArray.length;

Is that a syntax thing?? I will hard code it in for now though

##### Share on other sites

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

##### Share on other sites

RoboticdogGame.OrangeMonstars.prototype.shoot = function(x,y) {

I tried x,y ... didn't take

##### Share on other sites

Sorry! No that worked thanks!

I'll try what you said now..

##### Share on other sites

Ok, that sort of worked!

But it doesn't utilize the anglesArray!  It just divides the length into the timer amount

##### Share on other sites

like this?

this.game.physics.arcade.velocityFromAngle( anglesArray[(this.game.time.totalElapsedSeconds() % shootingdelay) % anglesLen], -100, newElement.body.velocity);

i'll check

##### Share on other sites

hmm well, there was no velocity!??

##### Share on other sites

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:

And it shoots

##### Share on other sites

I'm not sure about using the game time, because what if I get the enemies to respawn?