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***********************************
**********************************templeRat Step Code***********************************
Version: Gamemaker 8.1.135 (r6892)
Enemy Object: templeRat
Spawning Object: objGhostTutorialCoffin
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;
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);
}
Enemy Object: templeRat
Spawning Object: objGhostTutorialCoffin
Last edited by a moderator: