Enemy falls through floor?

I've got an enemy that runs back and forth on a platform

create:
GML:
image_speed = 1;
dir = -1;
en_movespeed = 2;
grav = .2
en_xspd = 0;
en_yspd = 0;
grounded = true;
aoh = true;
step:
GML:
//gravity and speed
if (en_yspd < 10) en_yspd += grav 
en_xspd = en_movespeed * -dir
image_xscale = sign(-en_xspd)
 
//Horizontal Collision
if (place_meeting(x+en_xspd,y,ob_wall))
{   
    while(!place_meeting(x+sign(en_xspd),y,ob_wall))
    {
        x += sign(en_xspd);
    }
    
     en_xspd = 0;
}
x += en_xspd;

 
//Vertical Collision
if (place_meeting(x,y+en_yspd,ob_wall))
{
    while(!place_meeting(x,y+sign(en_yspd),ob_wall))
    {
         y += sign(en_yspd); 
    }
    en_yspd = 0;}

y += en_yspd;  


//keep on platform

 if grounded and aoh and (!place_meeting(x+en_xspd,y+16,ob_wall))
{ 
    en_movespeed = -en_movespeed;
    
}
Parent to enemy_shell (decides whether the death is a skelton or explosion)

GML:
if place_meeting(x,y,ob_hitbox)
{if explode == true {instance_change(ob_explosion,true)}
}

if skeleton == true {instance_change(ob_skeleton,true)}
All of that works, in that they will drop to the floor and run in the right direction and turn around at the edge, but it only seems to work on the floor I'm on as the player. I didn't realize this was a problem as I was debugging the code with oll the objects on the same platform.

I don't see my mistake. What could be causing this?

Any help appreciated. Thanks.
 

Nidoking

Member
Are you doing something to handle the player's collisions with walls, like making them jump-through when the player's not standing on them? You've got a lot of extra variables that don't seem to do anything (grounded, aoh...), so either you haven't posted everything or you are yourself confused about what you need.
 

NightFrost

Member
Sounds like there might be a problem with your other platform's collision boxes. Make sure they've been defined correctly, and make sure you're not following that Spaldings' tutorial on one-way platforms where collisions get turned off...
 
Are you doing something to handle the player's collisions with walls, like making them jump-through when the player's not standing on them? You've got a lot of extra variables that don't seem to do anything (grounded, aoh...), so either you haven't posted everything or you are yourself confused about what you need.
Okay. Thank you. I realize now that I'm using jump-throughs and I'm turning off the mask when the player dips below the platform (It's a vertical platformer). The parent object is ob_wall, though.

I was thinking that turning the mask off was only going to affect the player object , and the parent object would save the enemy from falling. I'm now thinking that's not true. lol.

Here is the jump-through code:

create:
Code:
sprite_index = -1;
step:
GML:
/// @description Insert description here
// You can write your code in this editor
if (instance_exists(ob_player)) {
    if ob_player.bbox_bottom >  y
     {
        mask_index = -1;
    }
    else
    { if ob_player.bbox_bottom < y
        mask_index = sp_jumpthrough;  
    }
}

draw:
[CODE=gml]draw_sprite(sp_jumpthrough,0,x,y)

How would I code the enemy or the jump-through to prevent this?
 

NightFrost

Member
The basic idea behind one-way platforms is to do a separate collision check against completely solid platforms and one-way platforms. You handle the collision process if either player collides with a solid platform, or collides with a one-way platform while they're above it (player y is smaller than platform y) and falling down (their vertical speed is higher than zero). Incidentally, this is another reason why player sprite's origin is placed at their feet; when they collide with one-way while falling and player y is smaller than platform y, it is guaranteed that they are visually above the platform.
 

TheouAegis

Member
For starters, the ground should have no code in it. It's just the ground. lol You could still play with mask_index, but the player and any jumping enemy would need to toggle masks for all ground off and on each step, which is a lot of overhead for the program.

Your best option is to actually use standard jump-through logic:
  1. is the instance falling?
  2. is there a collision below the instance?
  3. is there NOT a collision at the current position?
  4. if yes to all 3, collide with pass-through.
šŸ„·
 
Top