1 decade ago by KenD
I've got my player able to jump and slide, and am updating the collision box appropriately, but I'm having trouble preventing him from falling through the floor after a jump. I've read all the threads on crouching, and I actually have that working fine. I'm correcting for the size, offset and position after finishing the slide animation in the update method like so:
That technique isn't very flexible though, since I'd like to be able to stop sliding if the player runs into something. Same thing for jumping. So I tried putting this code in handleMovementTrace:
The call to
if (!this.isSliding) { // normal movement/animation stuff } else { if (this.anims.slide.loopCount > 0) { this.isSliding = false; this.size = this.normal.size; this.offset = this.normal.offset; this.pos.y = this.pos.y - 70; this.anims.slide.rewind(); } }
That technique isn't very flexible though, since I'd like to be able to stop sliding if the player runs into something. Same thing for jumping. So I tried putting this code in handleMovementTrace:
handleMovementTrace: function(res) { if (this.isJumping && (res.collision.y || res.collision.slope)) { console.log("Fixing collision"); console.log(res); this.isJumping = false; this.size = this.normal.size; this.offset = this.normal.offset; this.pos.y = this.pos.y - 70; this.vel.y = 0; this.accel.y = 0; } this.parent(res); }
The call to
this.parent(res)
basically negates the rest of the function, and my character will fall through the floor. I can fix that by placing it in an else
block, but after the jump is complete, the animation looks choppy since he's being forced up 70 pixels (the animation sheet is drawn so he has a smooth landing). So my question is, how can I have a smooth jump animation and still resize the collision box appropriately? Do I need to update the animation sheet so the character's jump animation is at the bottom of the tile, instead of the top? Or is there a better way that I'm just not getting?