zywaa Posted April 19, 2015 Share Posted April 19, 2015 Hello, 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 More sharing options...
rich Posted April 20, 2015 Share Posted April 20, 2015 http://phaser.io/examples/v2/arcade-physics/offset-bounding-box Link to comment Share on other sites More sharing options...
JTronLabs Posted August 27, 2016 Share Posted August 27, 2016 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) ); anthkris 1 Link to comment Share on other sites More sharing options...
anthkris Posted February 25, 2017 Share Posted February 25, 2017 @JTronLabs I was bedeviled by a problem in my new game where, on reseting the sprite in a pool of objects, the body sizes were all super wonky. Your fix was the ticket! Thanks so much for sharing! JTronLabs 1 Link to comment Share on other sites More sharing options...
JTronLabs Posted March 8, 2017 Share Posted March 8, 2017 Glad it helped. I think setCircle is not relative to scale, in case you run into that issue Link to comment Share on other sites More sharing options...
Recommended Posts