Pushing objects

I’ve decided that I wanted to add the ability for the character to push crates around to help him get through the level. Although, I’ve been having big trouble with this. Whenever the character moves away after pushing the crate, it trails behind him like it’s attached to him. with multiple crates, I will push each crate, but every crate after the 1st one causes the the 1st to move instead. How can I fix these problem?

You’ll have to go into greater detail, and provide a little code too. Please only post relevant code, though.

I would post the code, but it’s absolutely atrocious. I’ll just explain the method that I have for their movement. For every update of the player, I run a function that checks for collisions b/n the crates and the player and makes a list of what crates are colliding with the player and mark those crates with a variable called “beingPushed”. Then, when I update the crates, if they are beingPushed, then it sets their horizontal movement speed to the same as the player. Otherwise it sets the crate’s HM to 0.

while doing the push, a simple fix could be check to see if the the character changes direction, maybe store the X variable to like oldX

Basically just check if the X is changing, if X is increasing, check to see if X all of a sudden becomes negative, so


if (player.X < oldX){
beingPushed = false;
}else{
oldX = player.X
}

But how would that work if the character can move either way?

use a switch statement maybe just before, do some checks, figure out what way the character is walking, then push it into a switch, let the switch hold the


case a:
if(player.X < oldX){
beingPushed = false;
}
break;

case b:
if(player.X > oldX){
beingPushed = false;
}
break;