GMS 2 Need help with centering bullets [SOLVED]

Discussion in 'Programming' started by Zabicka, Jan 9, 2019.

  1. Zabicka

    Zabicka Member

    Joined:
    Dec 14, 2018
    Posts:
    29
    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.

    [​IMG]
    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;
    
     
  2. Sam04

    Sam04 Member

    Joined:
    Nov 26, 2018
    Posts:
    27
    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 likes this.
  3. Zabicka

    Zabicka Member

    Joined:
    Dec 14, 2018
    Posts:
    29
    Thank you so much, and thank you for your advice!
     
    Sam04 likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice