# Squishing a player sprite between two obstacle sprites

Hi! I’m trying to make a pair of obstacles that slowly move from opposite ends of the screen and compress the player to end the game if they happen to be between them when they touch.

This is the approach I’ve tried:

squisherL.velocityX = 1;
squisherR.velocityX = -1;
squisherL.displace(kurt);
squisherR.displace(kurt);
if (squisherL.isTouching(squisherR)){
squisherL.velocity = 0;
squisherR.velocity = 0;
if (squisherL.isTouching(kurt) && squisherR.isTouching(kurt)){
//previously, this condition was
// if (kurt.x >= squisherL.x && kurt.x <= squisherR.x){
// but this did not work any better because the displacement pushed
// him out and through the other squisher
health = 0;
}
}

I can tell from the Watchers that both kurt and squisherL are still objects, but that line gets highlighted with an error: ERROR: Line: 61: TypeError: this.velocity.magSq is not a function

Here is the code I have so far.

can’t tell you for certain what the error is but i assume that the collision is probably setting the velocity to a non number like Infinity which the velocity prototype properties cannot handle, to fix this i just made it stop when both walls are touching kurt

``````if(squisherL.isTouching(squisherR)) {
// kurt escaped somehow
squisherL.velocity = 0;
squisherR.velocity = 0;
} else if (squisherL.isTouching(kurt) && squisherR.isTouching(kurt)) {
// kurt died
squisherL.velocity = 0;
squisherR.velocity = 0;
kurt.scale = 0.08
health = 0;
}
``````

by using this approach we can tell if kurt progresses or if he got caught between the two walls and suffocated hope this helps

# Varrience

Thank you for your feeback. This approach is similar to one I have tried, but it results in kurt being pushed out from between the two squishers without losing any health.

I get a similar result if I’m checking their x properties.

``````if(squisherL.isTouching(squisherR)) {
squisherL.velocity = 0;
squisherR.velocity = 0;

if(kurt.x >= squisherL.x && kurt.x <= squisherR.x){
// kurt died
squisherL.velocity = 0;
squisherR.velocity = 0;
kurt.scale = 0.08
health = 0;
}
``````

It’s no big deal if he gets pushed out because I can always darken the screen and show Game Over when his health reaches 0, but the health doesn’t change unless one of the squishers is not actually pushing him.

Maybe I just have to disable collision with one of the squishers at the last possible second, but this could lead to a glitch where the player is able to run out of the trap.

Here is a version of the code which works only because the squisherR doesn’t push kurt (but is a lousy trap because he can easily avoid it).

that’s odd for me the collisions didn’t do that well anyways to ensure the clipping doesn’t take place use this instead

``````kurt.collide(helper);
if (squisherL.isTouching(squisherR)) {
// kurt escaped somehow
squisherL.velocity = 0;
squisherR.velocity = 0;
} else if (squisherL.isTouching(kurt) && squisherR.isTouching(kurt)) {
// kurt died
squisherL.velocity = 0;
squisherR.velocity = 0;
health = 0;
} else {
squisherL.displace(kurt);
squisherR.displace(kurt);
}
``````

once both walls are touching kurt or each other they will no longer displace kurt of which won’t squeeze him out of it

1 Like