Jump to content

Absolute position of sprite


Fredrik
 Share

Recommended Posts

How can I get the absolute position of a Sprite or Image in the world? I have a Sprite that is inside a container with scrollFactor of 0 and I want to move other sprites outside the container to that position. 

Basically this question but for Phaser 3 =)  - http://www.html5gamedevs.com/topic/5306-absolute-position-of-sprite/

Link to comment
Share on other sites

I can use the container.x & .y but it doesn't really work in my case. The container is drawn with scrollFactor 0, so when the camera moves the container does not move relative to the camera, but it does relative to the world. Thus the real world coordinates changes when the camera moves but the x & y fields in the container does not seem to update with the camera (they are what they were set to originally). 

Example: Initially the container is placed at 0,0. Then the player and camera scrolls 200px to the right. Now the real world position of the container is 200,0 but .x,.y still says 0,0. 

Hope that makes sense! ?

Link to comment
Share on other sites

I don't know if these are the _right_ answers but the two things that come to mind:

1. Manually compute the world position by taking objects in scrollFactor(0) position and adding in .scrollX and .scrollY

2. If you have access to the camera for the scene you want to position things in you can use getWorldPoint.

Both approaches should let you move between screen and world positioning.

Link to comment
Share on other sites

I am playing around with this.scene.cameras.main.scrollX now, it seems to give me the current scroll offset relative to the world and from that I can add the container position etc. So I think this will work. I tried getWorldPoint as well and it gives me the same result (for camera position). 

It would be nice with some more intuitive way of doing it though ?

Link to comment
Share on other sites

  • 1 year later...

I'm not sure if it's just me but it seems objects arbitrarily mix themselves with whether or not their ".x" will use local or world coords. Without an easy way to convert between the two even the simplest operations in phaser3 seem counter-intuitive. Or could someone  kindly explain what I'm doing wrong?

Link to comment
Share on other sites

  • 1 month later...
 Share

  • Recently Browsing   0 members

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