1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

[SOLVED] Help with destroying an instance

Discussion in 'Programming' started by oldnoob, Feb 18, 2019.

  1. oldnoob

    oldnoob Member

    Joined:
    Jun 27, 2016
    Posts:
    12
    Hello there,

    Platformer question:

    I'm having trouble destroying an instance of an object my player is sanding on.The sprite connected to the obj_blockcollapse has 9 frames of animation and the object's create event sets both the image_index and image_speed to 0.

    The code in my player's step event is:
    PHP:
    //check if player is on collapsing block

    if place_meeting(x,y+1,obj_blockcollapse){
        var 
    collapse=instance_place(x,y+1,obj_blockcollapse);
        
    collapse.image_speed=.2;
        if 
    collapse.image_index==8 instance_destroy();
    }
    else{
        
    obj_blockcollapse.image_speed=0;   
    }
    I've tried using the collapse ID within the instance_destroy brackets (together with true/false), but I understand this shouldn't matter and it doesn't make a difference anyway.

    I'm a little stuck here now so any help much appreciated.

    Many thanks.


    OldNoob
     
  2. Turkish Coffee

    Turkish Coffee Member

    Joined:
    Nov 25, 2016
    Posts:
    559
    change this "if collapse.image_index==8" to this "if collapse.image_index>=8" and "collapse.instance_destroy();" check if anything happens

    and probably last obj_blockcollapse to collapse too
     
  3. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    2,012
    Is your intent to have the blocks keep collapsing only when player is standing on them and pause otherwise, as that's what your code does at the moment. Depending on your level layout it might not matter, but if player stands on two or more collapsibles, your code makes only one of them collapse at a time. If necessary, this can be easily fixed by running the collapse check on the blocks instead.

    Also, it seems your instance_destroy is affecting the player.
     
    oldnoob likes this.
  4. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,142
    One block collapsing at a time is fine if you want that; that's how Castlevania games worked in the past. Either way, if you want the block to collapse even after the player as left it, such as if the player jumps off it, you should run all the code in the obj_blockcollapse itself.

    End Step Event:
    Code:
    if place_meeting(x,y-1,obj_player) && obj_player.bbox_bottom<=bbox_top
        image_speed = 0.2;
    else image_speed = 0;    //remove this if you want it to keep crumbling after the player leaves
    
    Animation End Event:
    Code:
    instance_destroy();
     
  5. NeZvers

    NeZvers Member

    Joined:
    Mar 24, 2018
    Posts:
    320
    I don't follow the reason for obj_player.bbox_bottom<=bbox_top since that means player is above or pixel overlap (basically the same as place_meeting(x, y-1, obj_player). I think it's not needed.
     
  6. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,142
    It's not the same at all. But yes, it's an optional line depending on how your game is coded. If you intentionally make it so that under no uncertain circumstances can the player ever go into obj_blockcollapsable from any direction, then the line isn't needed. But if the player can jump through obj_blockcollapsable from below or if the player can at least jump partway into it (like, it's okay to jump up into it as long as he wouldn't be able to jump completely through it), then you would need that check.
     
  7. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    2,012
    If the player uses states, another alternative for bbox check would be to check the state. If player is in jump state - that is, either jumping up or falling down - the check is skipped. In any other state player at y - 1 is in fact standing on the block (though this depends on state definitions) and it can collapse.

    In fact it occurs to me now that state might be a better check, as there is a small chance that player hits the check spot while jumping upwards, and causes the block to collapse while moving up past it... Or check that player's vertical speed is greater than zero, but it might be zero if this collision check runs after player is done with their movement & collision code.
     
  8. oldnoob

    oldnoob Member

    Joined:
    Jun 27, 2016
    Posts:
    12
    Thank you, all. Some really good advice there and most gratefully received.
    • I changed the image_index check to >= because... why not - makes sense!
    • Moved the collision code to the step event of the collapsing block so all blocks under my player's feet start to collapse and not just one as Mr NightFrost suggested.
    • My understanding of instance_destroy has improved and it now targets the desired object (rather than my player) - oops!
    I did intend for the blocks to remain 'mid-collapse' after the player had left them as in the classic game, Manic Miner:
    [​IMG]

    So, with place-holder graphics and no fine-tuning, my collapsing blocks work:
    [​IMG]

    So thank you, all. I've learnt a lot in this thread.

    :)


    OldNoob.
     

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