[SOLVED] Help with destroying an instance

oldnoob

Member
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
 
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
 

NightFrost

Member
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.
 

TheouAegis

Member
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();
 

NeZvers

Member
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();
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.
 

TheouAegis

Member
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.
 

NightFrost

Member
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.
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.
 

oldnoob

Member
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:


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


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

:)


OldNoob.
 
Top