GameMaker Making a sprite disappear

E

Erutan Egaro

Guest
Hello, I am currently working on a side scroller "army vs army" in the same branch as the "Commando Assault" series, but with a bit more control over your troops, and covers to create a trench-like combat style.

For this purpose I wanted to make the player able to designate certain covers as "check points", where the troops would stay stationed, to defend a position or to wait the order to charge.
Those points would be created by a simple left mouse's click on a selected cover, and a sprite of a flag (or something else later, I'm not really thinking about the sprites yet) would pop on the current cover deisgnated as "checkpoint". This works, except for one part.
I haven't been able to make the flag disappears in any way when the cover is not designated as the "checkpoint" anymore. Here is the code of the "cover" object, in the "left mouse pressed" event:

Code:
var lay_id = layer_get_id("la_flag_sprite"); //part of the code dedicated to delete the precedent sprite each time left mouse is pressed again

if (layer_sprite_exists(lay_id, spr_flag1)){
    layer_sprite_destroy(lay_id);
} 

//everything past this point works

obj_cover.flag1 =false;

flag1 = true;

layer_sprite_create("la_flag_sprite", x, y-sprite_height, spr_flag1);
As you can see, I've tried to toy around with the layer_sprite_destroy fonction, to make the precedent sprite disappear before the new one is created ("la_flag_sprite" is the layer's name)


Anyone has an answer? Thank you in advance
 

chamaeleon

Member
Hello, I am currently working on a side scroller "army vs army" in the same branch as the "Commando Assault" series, but with a bit more control over your troops, and covers to create a trench-like combat style.

For this purpose I wanted to make the player able to designate certain covers as "check points", where the troops would stay stationed, to defend a position or to wait the order to charge.
Those points would be created by a simple left mouse's click on a selected cover, and a sprite of a flag (or something else later, I'm not really thinking about the sprites yet) would pop on the current cover deisgnated as "checkpoint". This works, except for one part.
I haven't been able to make the flag disappears in any way when the cover is not designated as the "checkpoint" anymore. Here is the code of the "cover" object, in the "left mouse pressed" event:

Code:
var lay_id = layer_get_id("la_flag_sprite"); //part of the code dedicated to delete the precedent sprite each time left mouse is pressed again

if (layer_sprite_exists(lay_id, spr_flag1)){
    layer_sprite_destroy(lay_id);
}

//everything past this point works

obj_cover.flag1 =false;

flag1 = true;

layer_sprite_create("la_flag_sprite", x, y-sprite_height, spr_flag1);
As you can see, I've tried to toy around with the layer_sprite_destroy fonction, to make the precedent sprite disappear before the new one is created ("la_flag_sprite" is the layer's name)


Anyone has an answer? Thank you in advance
Is spr_flag1 a variable holding the return value of layer_sprite_create()? I'm guessing not, since your last line of code does not store the layer sprite element returned.
 
E

Erutan Egaro

Guest
Is spr_flag1 a variable holding the return value of layer_sprite_create()? I'm guessing not, since your last line of code does not store the layer sprite element returned.
Indeed, spr_flag1 is just the name of the flag sprite
 
R

Rugensoft

Guest
You want to get the flag away again, when it is beeing clicked again?

Make an event on the flagobject with LMB and say instance_destroy(). Would that work for you?

Edit:
If you want to display a new object after that, you can just proceed with the code in the event.
GML:
instance_create_layer(x, y, "Instances", obj_***)
 

NightFrost

Member
Both layer_sprite_exists and layer_sprite_destroy require unique sprite ID, not sprite name (check their manual pages). This layer sprite ID runs on the same idea as instance ID values. In this case, you're just getting it as return value from layer_sprite_create so you need to store it or lose it. (Or rediscover it by retrieving all flag sprites on the layer and then try to guess which one player wanted to remove.)
 
E

Erutan Egaro

Guest
Both layer_sprite_exists and layer_sprite_destroy require unique sprite ID, not sprite name (check their manual pages). This layer sprite ID runs on the same idea as instance ID values. In this case, you're just getting it as return value from layer_sprite_create so you need to store it or lose it. (Or rediscover it by retrieving all flag sprites on the layer and then try to guess which one player wanted to remove.)
How do you store the variable? I've tried to assign it to a variable flag_id, and then execute the _layer_sprite_destroy(flag_id) like this:
Code:
 flag_id = layer_sprite_create("la_flag_sprite", x, y-sprite_height, spr_flag1);
but it still didn't work, so either you can't store a value like that, or I'm just using the function the wrong way
 
E

Erutan Egaro

Guest
You want to get the flag away again, when it is beeing clicked again?

Make an event on the flagobject with LMB and say instance_destroy(). Would that work for you?

Edit:
If you want to display a new object after that, you can just proceed with the code in the event.
GML:
instance_create_layer(x, y, "Instances", obj_***)
I've already used the layer_sprite_destroy in that purpose, I've tried with instance_destroy and it didn't work too

as for the instance_create function, layer_sprite_create( visible on the original comment) serve the same purpose.
 
Top