Jump to content

Problem with Arcade body.SetSize()


Recommended Posts



i'm participating to the LudumDare #32 and i have a big issue with Phaser right now.
My player had some animations but i need to restrict his hitBox to the character only, but the function "body.setSize()" just doesn't work and the hitbox isn't changing at all.

I tried to add the sprite after and before enabling the physics.

I tried to modify the hitBox before and after loading a texture.

I tried with and without the "this.anchor.setTo()"

Nothing seems to work and i can't do what i need to do without this function and my game can't work neither, and that's sad :'(


Thanks in advance for your help and sorry for my poor english.


This is my Player Object: 

var Player = function(game){  Phaser.Sprite.call(this, game, game.width / 2 - 32, game.height - 195, 'player');  this.game.physics.arcade.enable(this);  this.body.collideWorldBounds = true;  this.body.setSize(66,82);  this.anchor.setTo(0, 0.5);  this.nb_cailloux = 0;  this.is_typing   = false;  this.speed       = 0  this.runAnim     = this.animations.add('run', [0,1,2,3,4,5], 6, true);  this.gunAnim     = this.animations.add('getGun', [6,7,8,9,10,11], 6, false);  this.gunRun      = this.animations.add('gunRun', [12,13,14,15,16,17], 6, false);  this.shoot       = this.animations.add('shoot', [6,7,8,9,10,11], 6, false);  this.gunAnim.onComplete.add(function() {    this.animations.play('gunRun', 6, true);  }, this);  this.animations.play('run', 6, true);  game.add.existing(this);};Player.prototype = Object.create(Phaser.Sprite.prototype);Player.prototype.constructor = Player;Player.prototype.update = function(){}
Link to comment
Share on other sites

  • 1 year later...

I know this thread is >1 year old but I've had the same trouble in multiple games, and have spent many, many hours trying to figure out the issue (still a n00b). Anyways, here's the issue. Basically the setSize method is relative to the sprite's scale (though this may change in the future, depending on what Rich responds to the GitHub issue). To fix this

this.body.setSize(this.width / this.scale.x,this.height / this.scale.y);

These values must be positive, since width/height's sign will match it's scale's sign it'll work. If you want to set it to an absolute value don't forget to take Math.abs of the scale

this.body.setSize(32 / Math.abs(this.scale.x),32 / Math.abs(this.scale.y) );


Link to comment
Share on other sites

  • 5 months later...
  • 2 weeks later...

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...