Hi guys,
I've just started playing around with Box2D in my HTML5 and JavaScript game and everything was fine until I got to implementing crouch. I understand that you can't resize bodies in Box2D and from what I gathered to change the size of my player entity to match the size of the crouch I'd need to destroy the shape, change the size attributes, and then recreate the shape. Is this correct?
Creating shapes seems to have worked...but I'm totally lost as to how to destroy. As in, I don't know what to actually reference in the DestroyShape() function. Currently my code looks like this:
How do I reference the shape I want to destroy before creating the new shape? If I remove the DestroShape() completely and run this with "this.setShapeDef()" only new shapes of the right size do seem to get created each time I press the crouch key. But they're created on top of my original full-size shape and presumably that needs to be destroyed before the new one is created...Or am I completely off base here?
I've just started playing around with Box2D in my HTML5 and JavaScript game and everything was fine until I got to implementing crouch. I understand that you can't resize bodies in Box2D and from what I gathered to change the size of my player entity to match the size of the crouch I'd need to destroy the shape, change the size attributes, and then recreate the shape. Is this correct?
Creating shapes seems to have worked...but I'm totally lost as to how to destroy. As in, I don't know what to actually reference in the DestroyShape() function. Currently my code looks like this:
Code:
// Crouch
if (ig.input.state('crouch') && this.standing) {
if (!this.crouching && !this.crouched){
console.log('destroy shape here');
this.body.DestroyShape(I DONT KNOW WTF TO PUT HERE);
this.crouching = true;
this.crouched = false;
}
if (this.crouching){
this.size.y = this.crouch;
console.log('set shape here');
this.setShapeDef();
this.crouching = false;
this.crouched = true;
}
this.currentAnim = this.anims.defaultCrouchIdle;
}
if (ig.input.state('crouch') && this.standing) {
if (!this.crouching && !this.crouched){
console.log('destroy shape here');
this.body.DestroyShape(I DONT KNOW WTF TO PUT HERE);
this.crouching = true;
this.crouched = false;
}
if (this.crouching){
this.size.y = this.crouch;
console.log('set shape here');
this.setShapeDef();
this.crouching = false;
this.crouched = true;
}
this.currentAnim = this.anims.defaultCrouchIdle;
}
How do I reference the shape I want to destroy before creating the new shape? If I remove the DestroShape() completely and run this with "this.setShapeDef()" only new shapes of the right size do seem to get created each time I press the crouch key. But they're created on top of my original full-size shape and presumably that needs to be destroyed before the new one is created...Or am I completely off base here?