ZoomBox Posted July 16, 2014 Share Posted July 16, 2014 Hello (it's me again) ! I have a problem with Sprite.body.setSize(width, height);Let me show you my extend Sprite:Brick=function(_state, _x, _y,){ Phaser.Sprite.call(this, _state.game, _x, _y); this.game.physics.enable(this, Phaser.Physics.ARCADE); this.loadTexture("brick"); this.game.add.existing(this);};Brick.prototype = Object.create(Phaser.Sprite.prototype);Brick.prototype.constructor = Brick;Brick.prototype.setDimensions=function(_width, _height){ this.width=_width; this.height=_height; this.body.setSize(_width,_height);}It's very basic here (I simplified) but when I call my Brick.setDimention(_width, _height), this sprite itself is correctly sized but the body is smaller. Check this screen: I'm not used to arcade physics and it's very strange.Thank you in advance. Link to comment Share on other sites More sharing options...
lewster32 Posted July 16, 2014 Share Posted July 16, 2014 Hmm, have you tried setting the body size first, then adjusting the size of the sprite afterwards?Brick.prototype.setDimensions=function(_width, _height){ this.body.setSize(_width,_height); this.width=_width; this.height=_height;} Link to comment Share on other sites More sharing options...
ZoomBox Posted July 16, 2014 Author Share Posted July 16, 2014 It's unefective Link to comment Share on other sites More sharing options...
lewster32 Posted July 16, 2014 Share Posted July 16, 2014 This seems very odd... are you using the latest version of Phaser? Is the sprite being scaled anywhere else in your game? Link to comment Share on other sites More sharing options...
ZoomBox Posted July 16, 2014 Author Share Posted July 16, 2014 Edit (we posted at the same time): This is the latest Phaser (2.0.6) and the sprite wasn't resized anywhere else. Thank you lewster32 for the time you often offer me. I found a solution but not the problem: I was loading the texture AFTER enabling Physics.By enabling the physics after, I'm abe to resize the entire thing without using Sprite.body.setSize(a, ;, setting the Sprite.width and Sprite.height is enought. So here it is:Brick=function(_state, _x, _y,){ Phaser.Sprite.call(this, _state.game, _x, _y); this.loadTexture("brick"); this.game.physics.enable(this, Phaser.Physics.ARCADE); this.game.add.existing(this);};Brick.prototype = Object.create(Phaser.Sprite.prototype);Brick.prototype.constructor = Brick;Brick.prototype.setDimensions=function(_width, _height){ this.width=_width; this.height=_height;}It doesn't explains why setSize() doesn't work properly, but... I suppose you're not allowed to create a physic body before setting a texture. Thank you again ! Link to comment Share on other sites More sharing options...
lewster32 Posted July 16, 2014 Share Posted July 16, 2014 Ahh I didn't spot this. Not sure exactly why myself, but it's handy to know! Nice work Link to comment Share on other sites More sharing options...
JTronLabs Posted August 27, 2016 Share Posted August 27, 2016 This is an old topic, and @ZoomBox found a suitable workaround. But I still had troubles. My solution is below. You basically need to divide by the Sprites scale, as setBody is relatively sized under the hood. Link to comment Share on other sites More sharing options...
bkirvin Posted September 27, 2016 Share Posted September 27, 2016 I've noticed that "this.player.anchor.setTo(0.5);" will create a bounding box centered on the player.body in Phaser v2.2.2 when "this.game.debug.body(this.player);" is used, but in the latest Phaser v2.6.2, "this.game.debug.body(this.player);" shows a bounding box anchored at top left even if "this.player.anchor.setTo(0.5);" is previously applied. Was this an intentional change or a bug in the new version? Link to comment Share on other sites More sharing options...
Recommended Posts