• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Need help with centering bullets [SOLVED]

Zabicka

Member
I'm using for loops to create multiple bullets with direction space between them, but the space between bullets is not centered and I don't know how to fix it.


Code:
case bullet.double:
        for (var i = 0; i < 20; i += 10) {
            with (instance_create_layer(x, y, "Bullets", oBullet)) {
                direction = 270 + i;
                speed = 5;
            }
        }
    break;
 

Sam04

Member
Your code creates a bullet with direction 270 and a second one with direction 270+10. If the difference between those two bullets is 10, that means the middle is 5, therefore the true center of what you coded is 270+5. The problem is that your first bullet is created in the place you want the center to be instead of creating it a bit to the side.

In this particular case where you already know beforehand in which direction the double shoot is made (270) and how much the separation is (10) then you can adjust accordingly. You would need to change it to:
Code:
direction = 265+i;
Another solution would be to change the for statements to something like this:
Code:
for (var i = -5; i < 6; i += 10) {
And you can leave the "direction = 270 + i;" just as it is.

As a side note, if you ever want to play with different values, you could try using more variables to set up more details. Like:
Code:
//whatever was before your example code
case bullet.double: {
    var directionToShoot = whateverDirectionYouWantToShoot; //Replace this with the actual direction
    var numberOfBullets = 2;
    var angleBetweenBullets = 10;
    var directionToStartCreatingBullets = directionToShoot - angleBetweenBullets*numberOfBullets/2;//You don't need to use () anywhere, operation order already takes care of everything.
    for (i = 0; i < numberOfBullets; i++) {
        with (instance_create_layer(x, y, "Bullets", oBullet)) {
            direction = directionToStartCreatingBullets + i * angleBetweenBullets;
            speed = 5;
        }
    }
break;}
//whatever was after your example code
This should always work and allow you to experiment easily with different settings.

Hope that helps.

Edit: By the way, it took me a while (and several reads) to understand the problem. Next time try to clarify even more details in your post. Like specifying that the square in the top is the one shooting downwards, or what do you meant with "centered".
 

Zabicka

Member
Your code creates a bullet with direction 270 and a second one with direction 270+10. If the difference between those two bullets is 10, that means the middle is 5, therefore the true center of what you coded is 270+5. The problem is that your first bullet is created in the place you want the center to be instead of creating it a bit to the side.

In this particular case where you already know beforehand in which direction the double shoot is made (270) and how much the separation is (10) then you can adjust accordingly. You would need to change it to:
Code:
direction = 265+i;
Another solution would be to change the for statements to something like this:
Code:
for (var i = -5; i < 6; i += 10) {
And you can leave the "direction = 270 + i;" just as it is.

As a side note, if you ever want to play with different values, you could try using more variables to set up more details. Like:
Code:
//whatever was before your example code
case bullet.double: {
    var directionToShoot = whateverDirectionYouWantToShoot; //Replace this with the actual direction
    var numberOfBullets = 2;
    var angleBetweenBullets = 10;
    var directionToStartCreatingBullets = directionToShoot - angleBetweenBullets*numberOfBullets/2;//You don't need to use () anywhere, operation order already takes care of everything.
    for (i = 0; i < numberOfBullets; i++) {
        with (instance_create_layer(x, y, "Bullets", oBullet)) {
            direction = directionToStartCreatingBullets + i * angleBetweenBullets;
            speed = 5;
        }
    }
break;}
//whatever was after your example code
This should always work and allow you to experiment easily with different settings.

Hope that helps.

Edit: By the way, it took me a while (and several reads) to understand the problem. Next time try to clarify even more details in your post. Like specifying that the square in the top is the one shooting downwards, or what do you meant with "centered".
Thank you so much, and thank you for your advice!
 
Top