GMS 2 layer_create vs. layer_destroy vs. layer_sprite_destroy

Discussion in 'Programming' started by camerakid, Sep 12, 2018 at 10:40 AM.

  1. camerakid

    camerakid Member

    Joined:
    Jun 20, 2016
    Posts:
    233
    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
     
  2. Relic

    Relic Member

    Joined:
    Jun 27, 2017
    Posts:
    216
    layer_destroy() requires the Id of the layer. You are passing the depth of the layer- not it’s id.

    You should try layer_destroy(global.asset_layer)
     
    Deadboy86 and camerakid like this.
  3. camerakid

    camerakid Member

    Joined:
    Jun 20, 2016
    Posts:
    233
    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: Sep 12, 2018 at 10:52 AM
  4. Death

    Death Member

    Joined:
    Aug 8, 2018
    Posts:
    18
    Syntax: ...= x and...
    You are not comparing, but assigning, should be ...== x &&...
    same with y
     
  5. camerakid

    camerakid Member

    Joined:
    Jun 20, 2016
    Posts:
    233
    Thank you but it is the same still :-/
     
  6. Death

    Death Member

    Joined:
    Aug 8, 2018
    Posts:
    18
    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...
     
  7. Relic

    Relic Member

    Joined:
    Jun 27, 2017
    Posts:
    216
    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: Sep 13, 2018 at 12:55 AM
    camerakid likes this.
  8. camerakid

    camerakid Member

    Joined:
    Jun 20, 2016
    Posts:
    233
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice