SOLVED (HELP) My asteroids in Space Rocks are glitching

S

Someone

Guest
I made Space Rocks in GML and added a Collision event with other asteroids that makes both of the objects change direction:
GML:
direction += 180;
But when I hit them with my bullets they are glitching. I tried to add the variable
GML:
collisionInvulnerability = 0;
to the asteroid's create event and in the User event that fragments the asteroids and destroys them I added 1 to the variable
GML:
instance_destroy();

score += 10;

repeat(irandom_range(6,10)){
    instance_create_layer(x, y, "Instances", obj_debris);

}
  
if(sprite_index == spr_asteroid_big){
    repeat(2){
        var newAsteroid = instance_create_layer(x, y, "Instances", obj_asteroid);
        newAsteroid.sprite_index = spr_asteroid_med;
    }
  
}
  
else if(sprite_index == spr_asteroid_med){
    repeat(2){
        var newAsteroid = instance_create_layer(x, y, "Instances", obj_asteroid);
        newAsteroid.sprite_index = spr_asteroid_small;
          
    }
  
}

alarm[0] = 5*room_speed;

audio_play_sound(snd_hurt, 1, false);

collisionInvulnerabilty += 1;
and sent an alarm to reset the variable to 0. Then I added a condition to the collision with other asteroids that makes it only run when the variable is equal to 0:
GML:
if(!collisionInvulnerabilty == 0) exit;

direction += 180;
This didn't work and I really need to solve this problem before going on. Thank you so much in anticipation
 
Last edited by a moderator:

Nidoking

Member
So, the first thing in your user event is
instance_destroy();
This destroys the instance. Later in the event, you set
collisionInvulnerabilty += 1;
in the destroyed instance. This is like writing an important piece of information in a notebook that you've already thrown into a fire. Likewise,
alarm[0] = 5*room_speed;
this alarm will never happen because it takes 5 * room_speed steps to trigger, while your instance is destroyed after zero steps. Presumably, you want to perform these actions with the new instances you've created.

Finally, this is not the correct way to write this comparison:
if(!collisionInvulnerabilty == 0)
This does not mean "if collisionInvulnerability is not equal to zero". It means "if the logical negation of collisionInvulnerability is equal to zero". As long as false is equal to zero, it may still function the way you want, but this is a universal rule. If you ever write something like "== true" or "!= false", then you are certainly doing something wrong. To check that something is equal to true, just put it alone in the if. To check if it's equal to false, put not it in the if. (!thing_i_want_to_check). And to check if it is not the case that something is equal, use the != comparison operator. Just not with true, false, or any numeric value that you want to treat as true or false. Just put true or false in the variable.
 
S

Someone

Guest
So, the first thing in your user event is


This destroys the instance. Later in the event, you set


in the destroyed instance. This is like writing an important piece of information in a notebook that you've already thrown into a fire. Likewise,


this alarm will never happen because it takes 5 * room_speed steps to trigger, while your instance is destroyed after zero steps. Presumably, you want to perform these actions with the new instances you've created.

Finally, this is not the correct way to write this comparison:


This does not mean "if collisionInvulnerability is not equal to zero". It means "if the logical negation of collisionInvulnerability is equal to zero". As long as false is equal to zero, it may still function the way you want, but this is a universal rule. If you ever write something like "== true" or "!= false", then you are certainly doing something wrong. To check that something is equal to true, just put it alone in the if. To check if it's equal to false, put not it in the if. (!thing_i_want_to_check). And to check if it is not the case that something is equal, use the != comparison operator. Just not with true, false, or any numeric value that you want to treat as true or false. Just put true or false in the variable.
Thank you so much! I'm new to the world of programming and I was really struggling to find a solution. I really thank you a lot!
 
Top