P2 physical body does not collide after setRectangle () called phaserJS

I am creating a fish sprite. Collision with FoodCollisionGroup. Fish eat little food. The game works well. After that I resize it (increase the size of the fish) and it stops colliding with food. I think it might not be food collision due to the call to setRectangle. I have another problem that fishFoodCount doubles in size. how to solve these two problems

Fish.prototype.create = function(x, y){
    this.fish = game.add.sprite(x, y, this.spriteName);
    this.fish.physicsBodyType = Phaser.Physics.P2JS;
    game.physics.p2.enable(this.fish);
    this.fish.enableBody = true;
    this.fish.anchor.setTo(0.5,0.5);
    this.fish.body.collideWorldBounds = true;
    this.fish.animations.add('idle', [3 + (this.index * 8)], 10, false);
    this.fish.animations.add('move', this.getAnimationFramesArrayFor(this.index) , this.AnimationFrameRate, true);  
    this.fish.play('idle');
    this.fish.body.fixedRotation = true;
}

-------

Fish.prototype.setFishSize = function(width, height){
    this.fish.width = width;
    this.fish.height = height;
    this.fish.body.setRectangle(height/2, width/1.8, width/3.3, 0);
}

-------

Fish.prototype.eatFood = function(fish, food) {

    if(this.fishFoodCount > 9){
        this.setFishSize(game.width/25, game.height/4);
    }
    this.fishFoodCount += 1;
        food.sprite.kill();

}

      

+3


source to share


1 answer


You need to reapply the collision groups after use setRectangle()

.



+1


source







All Articles