MonsieurBlutbad Posted June 11, 2017 Share Posted June 11, 2017 Hello, I have a Sprite with 2 different frames which require different hitboxes for collision handling. I have created a group which consists of multiple sprites that form all hitboxes. Now I want to simply turn them on and off, depending on the frame of the parent Sprite. I thought this could be achieved with setting enableBody on the hitboxes to true or false. But it doesn't work. All hitboxes are active all the time. Any suggestions on how to make this work? Or is my approach wrong? export default class MySprite extends Phaser.Sprite { constructor(game, x, y) { super(game, x, y, 'my_sprite_key'); this.game = game; this.game.physics.arcade.enable(this); this.game.add.sprite(this); this.enableBody = true; this.body.immovable = true; this.hitBoxes = this.game.add.group(); this.hitBoxes.enableBody = true; this.hitBoxForFrame0 = this.hitBoxes.create(0, 0); this.hitBoxForFrame0.body.setSize(this.width * 0.5, this.height, -this.width, -this.height); this.hitBoxForFrame1 = this.hitBoxes.create(0, 0); this.hitBoxForFrame1.body.setSize(this.width * 0.5, this.height, -this.width * 0.5, -this.height); this.addChild(this.hitBoxes); this.setFrame(1); } setFrame(frame) { this.frame = frame; this.hitBoxForFrame0.enableBody = this.frame === 0; this.hitBoxForFrame1.enableBody = this.frame === 1; } } this.game.physics.arcade.overlap( this.bullets, this.mySprite.hitBoxes, function(bullet, hitBox) { console.log('Hit'); }, null, this ); Link to comment Share on other sites More sharing options...
squilibob Posted June 12, 2017 Share Posted June 12, 2017 I was under the impression that Arcade.enableBody was for creating the body and that to toggle whether a body checks for overlap you would use body.enable? this.hitBoxForFrame0.body.enable = this.frame === 0 MonsieurBlutbad 1 Link to comment Share on other sites More sharing options...
MonsieurBlutbad Posted June 12, 2017 Author Share Posted June 12, 2017 Yes, that's it! Thank you very much! Link to comment Share on other sites More sharing options...
snowbillr Posted June 12, 2017 Share Posted June 12, 2017 Another option would be to change what hitboxes your are passing in to the 'overlap' function based on the currently displayed frame. MonsieurBlutbad 1 Link to comment Share on other sites More sharing options...
MonsieurBlutbad Posted June 12, 2017 Author Share Posted June 12, 2017 I like that idea a lot, seems to be more in line with seperation of concerns than my approach. Very nice, thank you! snowbillr 1 Link to comment Share on other sites More sharing options...
Recommended Posts