• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED Gamemaker 8.1: Issue with enemy falling off ledges in one direction -- Gamemaker 8.1.135 (r6892)

G

GlitchyGamer

Guest
Greetings everyone, I am currently following Shaun Spaldings tutorial on creating platformer enemies and am having issues
with keeping enemies from falling off ledges.

The Intent: The current object that I'm working on is an enemy called templeRat. Ideally templeRat is just supposed to move right and left
changing direction when encountering a solid object or a ledge and damaging the player if it collides with him.

The Problem: The issue is that when the templeRat approaches the left ledge it turns around as intended, but when it approaches the right ledge it falls off.

Attempted Solutions: Reading the comments in Mr. Spaldings video, I am of the impression that when the templeRat turns right the sprite_width and
sprite_height variables somehow lose their value. If true then I do not know how to fix, as the templeRat object sprite has
its origin centered and I'm using a rectangular bounding box.

**********************************templeRat Create Code***********************************
Code:
/// Variables

///horizontal direction
dir = -1;
moveSpeed = 5;
grav = 20;
horizontalSpeed = 0;
verticalSpeed = 0;
/// flag value that determines whether or not enemy moves off ledge. ('0' means enemy falls off, '1' means enemy stops at ledge)
fearOfHeights = 1;
**********************************templeRat Step Code***********************************
Code:
/// set movement
horizontalSpeed = dir * moveSpeed;
/// set gravity
verticalSpeed += grav;

/// horizontal collision check
if (place_meeting(x + horizontalSpeed, y, objectSolidTile))
    {
        while(!place_meeting(x + sign(horizontalSpeed), y, objectSolidTile))
            {
                x += sign(horizontalSpeed);
            }
            horizontalSpeed = 0;
            /// change movement direction when ecountering an object
            dir *= -1
    }
x += horizontalSpeed;

/// vertical collision check
if (place_meeting(x, y + verticalSpeed, objectSolidTile))
    {
        while(!place_meeting(x, y + sign(verticalSpeed), objectSolidTile))
            {
                y += sign(verticalSpeed);
            }
            verticalSpeed = 0;
            /// turn object around if the sprite's width starts to overshoot the ledge
            if (fearOfHeights) and !position_meeting(x + (sprite_width/2)*dir, y + (sprite_height/2), objectSolidTile)
            {
                dir *= -1;
            }
    }
y += verticalSpeed;

if (horizontalSpeed != 0)
    {
    /// then have player sprite change direction according to movement
        image_xscale = sign (-dir);
    }
Version: Gamemaker 8.1.135 (r6892)

Enemy Object: templeRat
Spawning Object: objGhostTutorialCoffin
 
Last edited by a moderator:

Nidoking

Member
I am of the impression that when the templeRat turns right the sprite_width and sprite_height variables somehow lose their value
No, but if you use negative image_xscale, then sprite_width will be negative. You're failing to account for that because you're multiplying sprite_width by dir. One or the other is always negative. I think you either don't want to multiply by dir at all, or you always want to multiply by -1. One or the other.
 
G

GlitchyGamer

Guest
Adjustment: I went ahead and changed the 'dir' variable in the IF statement that checks for the ledge to always be negative as you advised.

Result: The templeRat now moves back and forth properly! Thanks for the help Nidoking!
 
Top