r/gamemaker•
blablobloo

[Help!] Getting all instances of an object to move

Resolved

I know there are a lot of threads and resources regarding this, but at least the ones I've found haven't been of help in my situation. On to the problem then.

This is a script:

var amount_of_bullets = 5;
var creation_offset = 48;

for (var i = 0; i < amount_of_bullets; i++) {
    var x_offset = random_range(-creation_offset * 2, creation_offset * 2);
    var y_offset = random_range(-creation_offset, creation_offset);
    
    bullet = instance_create(x + x_offset, y + y_offset, obj_enemy_test_bullet);
    bullet.creator = id;
}

alarm[3] = room_speed / 2;

And this is the alarm[3]:

var bullet_spd = 8;
var target_offset = 64;

for (var i = 0; i < instance_number(obj_enemy_test_bullet); i++) {
    var target_x_offset = random_range(-target_offset, target_offset);
    var target_y_offset = random_range(-target_offset, target_offset);

    bullet_ref = instance_find(obj_enemy_test_bullet, i);
    
    var dir = point_direction(bullet_ref.x, bullet_ref.y, obj_player_battle.x + target_x_offset,                    obj_player_battle.y + target_y_offset);
        
    var xforce = lengthdir_x(bullet_spd, dir);
    var yforce = lengthdir_y(bullet_spd, dir);

    physics_apply_impulse(bullet_ref.x, bullet_ref.y, xforce, yforce);
}

Basically, in the script I create an amount of bullets, I want them to not move for half a second and then move towards the player. I had it in a working state without the delay, but I've been struggling with adding the delay. What happens right now is that the bullets just spawn but never move towards the player, they just stay put. I also tried using a with () statement rather than a for loop, with what I think were the appropriate changes, and what happened in that situation was that only one of the bullets started to move after the half a second delay.

So yeah, help please, and lots of thanks to anyone jumping in.

BTW, I'm very new to GM:S, so I'm still trying to learn the best way to set up things, if there is a better way to set up such a thing (the delay and then move), please let me know! Again, thanks.

[edit]: some code snippets were left behind.

Posted Aug 25, 2016, 2:40 AM UTC

23
Loading comments…