Jump to content

Body does not change when sprite is scaled


PicoFoundry
 Share

Recommended Posts

I may be missing something very simple, but I am quite new to using Phaser. I am creating a game where I have two platforms on the screen. I created a single sprite for the left platform, and tried flipping it to create the right platform, however, after doing this I found the player no longer collided with it. I rendered the body, and after moving the platform from the far right, found the body no longer matched with the sprite position. This can be seen in this image:

post-15157-0-23220200-1435787611.png

 

In my code I set this sprite up, like this:

this.rightBranch = this.add.sprite(this.game.width - 256, this.game.height, 'branch');this.rightBranch.scale.setTo(-1, 1);this.rightBranch.anchor.setTo(0, 1);this.game.physics.arcade.enable(this.rightBranch);this.rightBranch.body.immovable = true;

I could create a second sprite, flipped, and just use that, but I am curious whether I can scale the sprite and make sure the body is scaled with it.

post-15157-0-23220200-1435787611.png

Link to comment
Share on other sites

Hello,

 

tried it, does exactly what you say, but successfully made it to work, here is how:

 

When you are trying to scale on x-axis change anchor.x to 0.5, just tried it and it worked perfectly, basically make it scale through middle of your object therefore you leave your object and the body still overlapped (I suppose that scaling on y-axis needs y anchor set to 0.5 as well).

 

If you need your sprite with your anchor setting but still inverted I suggest firstly setting anchor to 0.5 while rendering it to buffer and than load it as a new sprite to your scene already inverted (safes performance during the game as well as solves your problem with changing the anchor) then set anchor on you new object to the value you need, either that or change your positioning with 0.5 anchor in mind, or something like that...

 

Let me know if this worked for you, I just tested it and everything was ok, so hopefully it solves your troubles too, ok?

 

btw: how exactly did you render the body (just curious)?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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