Jump to content

shooting at angles problem? help pls


nicwins
 Share

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:

 

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

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

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

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

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

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

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

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

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

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

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

  • Recently Browsing   0 members

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