Jump to content

Setting manually sprite.x and sprite.y


ZoomBox
 Share

Recommended Posts

Hello !

I have a quite big problem with physics.P2. I can't set sprite.x and sprite.y manually once the body is active.

Actually, I have no errors, but it just isn't effective (the sprite ignore my manual positionning).

 

I know I could do is with a sprite.reset(x,y); or with sprite.body.x and sprite.body.y but by those ways, it will reposition them from the center of the sprite and I don't want this.

 

Thank you in advance for your anwers.

Link to comment
Share on other sites

if you want to change how it positions the sprite in the world, you can change the sprite's anchor to another location like thus:

this.sprite.anchor.setTo(0, 0); //Upper left corner of the spritethis.sprite.anchor.setTo(1, 1); //Lower right corner of the sprite
Link to comment
Share on other sites

Sorry but once a Sprite is under p2 control you need to position it using physics properties (body values), otherwise you run the risk of tunnelling it through other bodies, screwing with local values like velocity/etc and generally freaking it out.

 

You can adjust the position of the body in relation to the sprite if you want to reset the anchor from being the center, but it's probably honestly a lot easier to just get used to knowing that x/y is the center point now and accounting for it in code.

Link to comment
Share on other sites

OK thank you for your quick answers.

 

@ctmartinez1992:

Yes... but no. It will just position my body and sprite with an offset. It's usefull but not in my case (I want them to keep the same anchor).

 

@rich:

OK, thank you.

What I did is to store de Width and Height value of my polygons (it could had been calculated once but it's less loading time if I do it myself).

So now when I want to position my sprite (and it's body), I set mySprite.body.x = myNewX + mySprite.getWidth();

Just what you said actually.

 

Thank you again. Bye :-)

(PS: Awesome engine)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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