Sending one type of object in 9 different way

6

66Gramms

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

Perseus

Not Medusa
Forum Staff
Moderator
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;
   }
}
 
6

66Gramms

Guest
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 by a moderator:

Perseus

Not Medusa
Forum Staff
Moderator
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.
 
Top