GameMaker layer_create vs. layer_destroy vs. layer_sprite_destroy

camerakid

Member
Hey Guys,

I have run into a strange behavior and not really understanding it.

GLOBAL LEFT PRESSED EVENT
Code:
global.asset_layer = layer_create(10);
GLOBAL RIGHT PRESSED EVENT
Code:
var destroy=layer_get_depth(global.asset_layer);
layer_destroy(destroy);
RESULT: Nothing happens but a warning message saying: "layer_destroy() - layer not found in current room"

Any idea why it is not finding the newly created layer?

Thanks.


Attila
 

camerakid

Member
Ah thank you I missed that.

EDIT: Actually how can I delete a sprite on an exact position from the same layer?

GLOBAL LEFT PRESSED
Code:
global.asset_layer = layer_create(10);
global.asset_spr   = layer_sprite_create(global.asset_layer, x,y,spr_01);
GLOBAL RIGHT PRESSED
Code:
var spr_id = layer_sprite_get_id(global.asset_layer, "spr_01");
if layer_sprite_get_x(spr_id) = x and layer_sprite_get_y(spr_id) = y
   {
   layer_sprite_destroy(spr_id)
   }
It is not destroying the sprite for me. Any idea why? :-/
 
Last edited:
D

Death

Guest
Syntax: ...= x and...
You are not comparing, but assigning, should be ...== x &&...
same with y
 
D

Death

Guest
Thank you but it is the same still :-/
if you corrected your if statement from:
if layer_sprite_get_x(spr_id) = x and layer_sprite_get_y(spr_id) = y
to:
if ((layer_sprite_get_x(spr_id) == x) && (layer_sprite_get_y(spr_id) == y)
then I am willing to bet that your x's and y's aren't equal...

try:
if ((floor(layer_sprite_get_x(spr_id)) == floor(x)) && floor((layer_sprite_get_y(spr_id)) == floor(y)))

If that works your x's and y's were different...
 

Relic

Member
While Death is correct that many languages require == for conditional checking, GML is not one of them so this won’t be the issue.

It’s more likely that where this code runs does not have an x and y equal to the location of the sprite. Where do you run this code? Why would it’s x and y position be at the same place as the sprite?

You probably wanted to check against the mouse_x and mouse_y coordinates. But even then, the chances of you clicking in the EXACT coordinates as the sprite origin are low. You can’t just click anywhere on the sprite using this code.

Edit: as I’m not fluent in the new asset layer functions I took a moment to read through them. Long story short, there is no precise way (using in built functions) to detect whether a mouse click occurs on a sprite in the asset layer. Is there some reason you are opposed to having this as an instance? If you create a new instance it will have mouse click events and collision masks that would make this task easier.
 
Last edited:

camerakid

Member
While Death is correct that many languages require == for conditional checking, GML is not one of them so this won’t be the issue.

It’s more likely that where this code runs does not have an x and y equal to the location of the sprite. Where do you run this code? Why would it’s x and y position be at the same place as the sprite?

You probably wanted to check against the mouse_x and mouse_y coordinates. But even then, the chances of you clicking in the EXACT coordinates as the sprite origin are low. You can’t just click anywhere on the sprite using this code.

Edit: as I’m not fluent in the new asset layer functions I took a moment to read through them. Long story short, there is no precise way (using in built functions) to detect whether a mouse click occurs on a sprite in the asset layer. Is there some reason you are opposed to having this as an instance? If you create a new instance it will have mouse click events and collision masks that would make this task easier.
Thank you for your support. Actually I use a snap-to-grid mouse script so I believe the click positions are quite exacts. Finally my solution is that I use a ds_grid I completely destroy the actual layer each time and redraw everything on it.
 
Top