Still can't use this correctly

W

Wild_West

Guest
What I thought would be easy to do for one of the last 10 monsters I need to complete my line up for the game was a little bug that spawns more bugs and the spawned ones would move around the center bug in a range of 200 pixels, acting as the nest_radius which is drawn in red in the image, and just move in random directions.

Trouble is despite mimicking the same exact thing I've seen done before I can't get these 2 functions to work how I need.
The bugs just all start at the same position and fly off, outside the radius.

So what am I messing up here because I can't see it.


if( distance_to_object(player) < 200 )
{
spawn_rate += 1;
if(spawn_rate = 20) and ( instance_number(hive_bug) < nest_number)
{
swarm = instance_create(x,y,hive_bug);
swarm.speed = 10;
swarm.direction = random(270);
swarm.x = x + lengthdir_x(nest_radius,direction);
swarm.y = y + lengthdir_y(nest_radius,direction);

spawn_rate = 0;
}
}
Game Bug is the spawner in the middle of the "Hive" red circle, and the hive bug is the one on the 0 degree area of the circle.
 
L

Latté

Guest
I think the problem might be that when it reads "swarm.x = x + lengthdir_x(nest_radius,direction)" it's interpereting "x" as the game bug's x, and "direction" as the game bug's direction. What happens if you do this?
Code:
if( distance_to_object(player) < 200 )
{
    spawn_rate += 1;
    if(spawn_rate = 20) and ( instance_number(hive_bug) < nest_number)
    {
       swarm = instance_create(x,y,hive_bug);
       swarm.speed = 10;
       swarm.direction = random(270);
       swarm.x = swarm.x + lengthdir_x(nest_radius, swarm.direction);
       swarm.y = swarm.y + lengthdir_y(nest_radius, swarm.direction);

       spawn_rate = 0;
   }
}
 
W

Wild_West

Guest
I think the problem might be that when it reads "swarm.x = x + lengthdir_x(nest_radius,direction)" it's interpereting "x" as the game bug's x, and "direction" as the game bug's direction. What happens if you do this?
Code:
if( distance_to_object(player) < 200 )
{
    spawn_rate += 1;
    if(spawn_rate = 20) and ( instance_number(hive_bug) < nest_number)
    {
       swarm = instance_create(x,y,hive_bug);
       swarm.speed = 10;
       swarm.direction = random(270);
       swarm.x = swarm.x + lengthdir_x(nest_radius, swarm.direction);
       swarm.y = swarm.y + lengthdir_y(nest_radius, swarm.direction);

       spawn_rate = 0;
   }
}
The spawned bugs just fly off from the top instead
 
L

Latté

Guest
Ok. Are they all flying directly up or do some of them fly slightly left and some fly slightly right? I think changing random(270) to random(360) and having them choose a new random direction after being spawned will fix part of it, but that won't stop them from flying away.
What does this do?
Code:
if( distance_to_object(player) < 200)
{
    spawn_rate += 1;
    if(spawn_rate = 20) and ( instance_number(hive_bug) < nest_number)
    {
       swarm = instance_create(x,y,hive_bug);
       swarm.speed = 10;
       swarm.direction = random(360);
       swarm.x = swarm.x + lengthdir_x(nest_radius, swarm.direction);
       swarm.y = swarm.y + lengthdir_y(nest_radius, swarm.direction);
       swarm.direction = random(360);

       spawn_rate = 0;
   }
}
If that works the way I think it will, the hive bugs should spawn in a circle around the game bug and go in a new random direction. This will not make them stay close to the bug that spawned them, though. If you need to make them "move around the center bug" you could try putting something in the step event of the hive bug object to make it move towards the game bug if its distance is greater than the nest radius, or instead of having them choose a new random direction after being spawned you could make them always point at a ninety-degree angle to the bug that spawned them.
Try this and see what it does.
game_bug step
Code:
if( distance_to_object(player) < 200)
{
    spawn_rate += 1;
    if(spawn_rate = 20) and ( instance_number(hive_bug) < nest_number)
    {
       swarm = instance_create(x,y,hive_bug);
       swarm.speed = 10;
       swarm.direction = random(360);
       swarm.x = swarm.x + lengthdir_x(nest_radius, swarm.direction);
       swarm.y = swarm.y + lengthdir_y(nest_radius, swarm.direction);
       swarm.spawner = id;

       spawn_rate = 0;
   }
}
hive_bug step
Code:
if( distance_to_object(spawner) > spawner.nest_radius)
{
    motion_add(point_direction(x,y,spawner.x,spawner.y),speed);
    speed = 10;
}
This will probably get the job done, but it might look odd.
 
W

Wild_West

Guest
Ok. Are they all flying directly up or do some of them fly slightly left and some fly slightly right? I think changing random(270) to random(360) and having them choose a new random direction after being spawned will fix part of it, but that won't stop them from flying away.
What does this do?
Code:
if( distance_to_object(player) < 200)
{
    spawn_rate += 1;
    if(spawn_rate = 20) and ( instance_number(hive_bug) < nest_number)
    {
       swarm = instance_create(x,y,hive_bug);
       swarm.speed = 10;
       swarm.direction = random(360);
       swarm.x = swarm.x + lengthdir_x(nest_radius, swarm.direction);
       swarm.y = swarm.y + lengthdir_y(nest_radius, swarm.direction);
       swarm.direction = random(360);

       spawn_rate = 0;
   }
}
If that works the way I think it will, the hive bugs should spawn in a circle around the game bug and go in a new random direction. This will not make them stay close to the bug that spawned them, though. If you need to make them "move around the center bug" you could try putting something in the step event of the hive bug object to make it move towards the game bug if its distance is greater than the nest radius, or instead of having them choose a new random direction after being spawned you could make them always point at a ninety-degree angle to the bug that spawned them.
Try this and see what it does.
game_bug step
Code:
if( distance_to_object(player) < 200)
{
    spawn_rate += 1;
    if(spawn_rate = 20) and ( instance_number(hive_bug) < nest_number)
    {
       swarm = instance_create(x,y,hive_bug);
       swarm.speed = 10;
       swarm.direction = random(360);
       swarm.x = swarm.x + lengthdir_x(nest_radius, swarm.direction);
       swarm.y = swarm.y + lengthdir_y(nest_radius, swarm.direction);
       swarm.spawner = id;

       spawn_rate = 0;
   }
}
hive_bug step
Code:
if( distance_to_object(spawner) > spawner.nest_radius)
{
    motion_add(point_direction(x,y,spawner.x,spawner.y),speed);
    speed = 10;
}
This will probably get the job done, but it might look odd.
It didn't work out at first but I took your advice about just controlling the movement through the hive bug instead of the spawner and it almost lookd right but only for a bit then they all just went flying like always, save for one that strangely stayed put
 
S

SyntaxError

Guest
ATM, you are creating them at a random direction, 200 pixels(nest_radius = 200; I assume) from the centre bug, not updating the x,y position after that but sending them off in the at a speed of 20 in a straight line based off the random direction.

You need to update the x, y (with length_dir_x/y) from within the hive_bug instances themselves.

Wher you create them
Code:
 swarm.parent = id;
Add in their step event,

Code:
direction += 10; //tweak this to suit.
x = parent.x + lengthdir_x(200, direction);
y = parent.y + lengthdir_y(200, direction);
If you use the objects for other things in your game, flag that code with an if check that is assigned when you create it.

eg:

Create event of hive_bug.
Code:
zoom_around_center_point_variable_flag_thingy = false;
When you create it with your above code, add.
Code:
swarm.zoom_around_center_point_variable_flag_thingy = true;
(remove the lengdth_dir_x/y all together and just create them at the x/y of the object creating them.

And in its step
Code:
if (zoom_around_center_point_variable_flag_thingy)
{
    direction +=choose(-1, 1) * 10;
    x = parent.x + lengthdir_x(200, direction);
    y = parent.y + lengthdir_y(200, direction);
}
That should sort you out.

EDIT: A few mistakes fixed
 
Last edited by a moderator:
W

Wild_West

Guest
ATM, you are creating them at a random direction, 200 pixels(nest_radius = 200; I assume) from the centre bug, not updating the x,y position after that but sending them off in the at a speed of 20 in a straight line based off the random direction.

You need to update the x, y (with length_dir_x/y) from within the hive_bug instances themselves.

Wher you create them
Code:
 swarm.parent = id;
Add in their step event,

Code:
direction += 10; //tweak this to suit.
x = parent.x + lengthdir_x(200, direction);
y = parent.y + lengthdir_y(200, direction);
If you use the objects for other things in your game, flag that code with an if check that is assigned when you create it.

eg:

Create event of hive_bug.
Code:
zoom_around_center_point_variable_flag_thingy = false;
When you create it with your above code, add.
Code:
swarm.zoom_around_center_point_variable_flag_thingy = true;
(remove the lengdth_dir_x/y all together and just create them at the x/y of the object creating them.

And in its step
Code:
if (zoom_around_center_point_variable_flag_thingy)
{
    direction +=choose(-1, 1) * 10;
    x = parent.x + lengthdir_x(200, direction);
    y = parent.y + lengthdir_y(200, direction);
}
That should sort you out.

EDIT: A few mistakes fixed
I was thinking of using choose for the other directional movement just wasn't sure if having that go back and forth in step wouldn't be too , if you'll pardon the joke "Buggy" when seen in game.
But it's working now so thanks ^^
 
Top