Help with Enemy Health Ring

pikachurian

Member
I am working on a game in gamemaker that has a ring which can be broken in different parts by the player. So far the ring can spin and the player can attack it. However, I can't figure out how to make it so the sword destroys the instances its touching. I tried using place meeting and the with statement, but that didn't work. Please help.
Question_1.PNG
 

gnysek

Member
I understand, that sword is attached to mouse position?

Easiest method is to:
- in sword, add collision with circle, and then "with (other) instance_destroy();" (can be DND icon if you're not using code).
- or, in circle, add collision with sword and just put "instance_destroy();"

Both should do same.
 

pikachurian

Member
The sword attacks when enter is pressed. I found a multi collision tutorial online and it seemed to fix it. Now it mostly works but I can't think of a way to limit how many circles the sword destroys with each attack. For example, if the sword's damage value is 3, then it should only destroy 3 circles. Sword's attack code:

var _hit_list = ds_list_create()
hit_count = instance_place_list(x,y,obj_enemy_point,_hit_list,true)
repeat(hit_count)
{
with(ds_list_find_value(_hit_list, 0))
{
instance_destroy()
}
ds_list_delete(_hit_list, 0)

//stops sword at collision
spd = 0

}
ds_list_destroy(_hit_list)
 

gnysek

Member
OK, so you've got a code which checks for INSTANCES AT POSITION x,y of obj_enemy_point (it's a white circle? it's a sword), and then destroys only first (but thanks to repeat - trying to destroy first several times...).

What you want is to destroy number of circles. So I understand, that those circles are a health? I can't think of a better way now (for sure there is), but this one comes to my mind:

Code:
var _points = [];
with (obj_enemy_points) {
_points[] = id; // "var" is shared with "with", without using other.
}

for(i=0; i<3; i++) {
  instance_destroy(_points[i]);
}
I'm leaving to you checking that array is longer than number of objects you want to destroy, so "out of bounds" error will happen.
 

davawen

Member
I think to destroy the number of circles you want, you might want to use instance_nearest(), which will give you the id of the closest instance of an object, so that when you attack, you get the position you want to attack and you destroy the nearest circles to the sword a fixed amount of time.
Here is what I mean in code :
Code:
///This happen when you attack
x = mouse_x, //I'm using the mouse coordinate for the attack but you can use
y = mouse_y; //any coordinate on the screen
  
repeat(hit_count)
{
    var circle = instance_nearest(obj_circle);
    if(point_distance(x, y, circle.x, circle.y) < hit_range)
    {
        instance_destroy(circle);
    }
}
 

NightFrost

Member
I'm seeing bit of mixed messages. Initially you said you wanted to destroy instances that get collided, and later that you want to destroy a number of instances equal to the sword's damage value. If it is the latter, a collision doesn't really matter, you just animate the sword to some approximation of swing and destroy spheres. Looking a the image, the sword probably just stabs directly upwards, so it's not like it can miss. Time the destruct to the point where the sword is at the hit portion of the attack.

(Or is there some player skill involved in aiming and timing and you still want collision checks for that?)
 

pikachurian

Member
The ring of circles rotates. If the player misses it makes it easier for them to land a hit later as there is now a bigger gap. Sorry about the confusion.
 

TheouAegis

Member
@pikachurian

Replace damage with the variable that determines your sword's strength.

Code:
var _hit_list = ds_list_create();
if instance_place_list(x,y,obj_enemy_point,_hit_list,true)
repeat(damage)
{
    var n = (ds_list_find_value(_hit_list, 0))
    if n
        with n instance_destroy();
    ds_list_delete(_hit_list, 0)
}
Note: The "if n" is necessary here because if the list is empty, it will return undefined and that will cause the calling instance (your sword) to destroy itself.
 
Top