GML Issues with wall collision, related to sprite index??

jjettii

Member
I'm having an issue where my player sometimes gets trapped by a wall and can't move. This only occurs when meeting with a wall to the left or right, at this point the player can't move up or down. The issue doesn't occur when meeting to a wall above or below the player.

The strangest part of all, this issue only occurs after adding some code to change the player sprite when moving. If I comment out the sprite code, the issue stops. How could the sprite index or image speed affect the issue with wall collision?

Here is my player code in the Step action. In the Create action, all I have is spd = 6.


// Set input variables
keyRight = keyboard_check(ord("D"));
keyLeft = keyboard_check(ord("A"));
keyUp = keyboard_check(ord("W"));
keyDown = keyboard_check(ord("S"));

// Resets input every step
moveX = 0;
moveY = 0;

// Assigns speed value to input
if(moveY == 0){ moveX = (keyRight - keyLeft) * spd; }
if(moveX == 0){ moveY = (keyDown - keyUp) * spd; }

// Horizontal collision
if(place_meeting((x + moveX), y, objWall)){
repeat(abs(moveX)){
if(!place_meeting(x + sign(moveX), y, objWall)){x += sign(moveX);}
else { break; }
}
moveX = 0;
}
// Vertical collision check
if(place_meeting(x, (y + moveY), objWall)){
repeat(abs(moveY)){
if(!place_meeting(x, y + sign(moveY), objWall)){y += sign(moveY);}
else { break; }
}
moveY = 0;
}

// Applies movement
if (global.gamepause = false) {
x += moveX;
y += moveY;
}

// Changes Sprite (This is the code I can comment out which removes the issue)
if moveX > 0 {
sprite_index = sprPlayerRight;
image_speed = 2;
}
if moveX < 0 {
sprite_index = sprPlayerLeft;
image_speed = 2;
}
if moveY > 0 {
sprite_index = sprPlayerDown;
image_speed = 2;
}
if moveY < 0 {
sprite_index = sprPlayerUp;
image_speed = 2;
}

if (moveX + moveY) = 0 {
image_speed = 0;
image_index = 0;
}

Movement working (with sprite-change code commented out):


Movement not working (includes the sprite-change code). When I hit the wall to the right, my Y movement doesn't work:
 

TheouAegis

Member
Double check the properties of all of your sprites. They must all have the exact same bounding box coordinates relative to the sprite's offset. If even one Sprite has even one value off, you will get stuck.
 

jjettii

Member
Double check the properties of all of your sprites. They must all have the exact same bounding box coordinates relative to the sprite's offset. If even one Sprite has even one value off, you will get stuck.
You're the best! I checked all the sprites and was able to find the culprit. All is back to working order :)
 
Top