1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice
  2. NOTICE: We will be applying a Xenforo update on Tuesday 25th of February. This means that from approximately 10:00 to 14:00 BST the forums will be offline (or possibly longer). Sorry for the inconvenience! Official Announcement here.

Sending one type of object in 9 different way

Discussion in 'Programming' started by 66Gramms, Feb 14, 2017.

  1. 66Gramms

    66Gramms Member

    Joined:
    Feb 2, 2017
    Posts:
    23
    Hi all!
    So I'm working on a shoot em up game and I want to make an enemy which creates 9 balls and send each in a direction (in the shape of a circle). I want to solve this without creating 9 different types of objects in the source. Do you have any idea? It's clear I have to use direction and speed but I don't know how to solve this... Thanks for the help in advance.
     
  2. Laurent57

    Laurent57 Member

    Joined:
    Nov 20, 2016
    Posts:
    154
  3. 66Gramms

    66Gramms Member

    Joined:
    Feb 2, 2017
    Posts:
    23
    Thank you
     
  4. Perseus

    Perseus Not Ragarnak Forum Staff Moderator

    Joined:
    Dec 29, 2016
    Posts:
    953
    You could do that with the help of a for statement. Use it to create 9 instances and set direction depending on the iteration each instance is created in. Since 360/9 = 40, you would use 40 as the base number and multiply it by the variable that the for statement depends on.

    Code:
    for (var i = 0; i < 9; i++) {
       with (instance_create(x, y, obj_Bullet)) {
          direction = 40 * i;
          speed = 4;
       }
    }
    
     
    66Gramms and Xer0botXer0 like this.
  5. 66Gramms

    66Gramms Member

    Joined:
    Feb 2, 2017
    Posts:
    23
    Thank you :) Btw it doesn't work as it tries to read i from the obj u used in with. I did it this way btw:

    for (i = 0; i < 10; i++)
    {
    dir += 36.5;
    nn=instance_create(x,y,obj_ball);
    nn.direction = dir;
    nn.speed = 8;
    }
     
    Last edited: Feb 18, 2017
  6. Perseus

    Perseus Not Ragarnak Forum Staff Moderator

    Joined:
    Dec 29, 2016
    Posts:
    953
    Note how I declared i using the var declaration. Doing that would make it a local variable and it would be available inside the with construction. But if you don't declare it that way, i would be declared as an instance variable of the instance calling the with construction, so it won't be available inside the with construction and GM would throw an error message. Had you used the var declaration, my code would have worked just fine.
     
    66Gramms likes this.
  7. 66Gramms

    66Gramms Member

    Joined:
    Feb 2, 2017
    Posts:
    23
    Oh sorry, my mistake.
     
    Perseus likes this.

Share This Page