Sign in to follow this  
Fredrik

Absolute position of sprite

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/

Share this post


Link to post
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! 😃

Share this post


Link to post
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.

Share this post


Link to post
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 😃

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
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.

Guest
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.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.