Jump to content

Body does not change when sprite is scaled


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:



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.


Link to comment
Share on other sites



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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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