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.
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
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
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.
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).