Sign in to follow this  
Ninjadoodle

Local to Global Question

Recommended Posts

Hi @enpu

I'm still working on my tiles inside container level. I've manged to get the interactivity and masks working, but now I have one more issue.

I'm moving a container around with a bunch of tiles in it. When I click a tile, I would like to add it to the parent container, at the same position it is currently visible on screen.

The problem is that it is adding it at the co-ordinates of the container it was inside originally.

Is there any local to global function, to make this easier?

Thank you in advance for any tips :)

Share this post


Link to post
Share on other sites

If you are moving your sprite from container to another (and both's world position is other than 0,0), you might need to convert your sprite position first to world position, then change it's parent and then convert it back to local position.

sprite.toWorldPosition(sprite.position); // Convert to world position
sprite.addTo(container); // Change parent
sprite.toWorldPosition(sprite.position, true); // Convert to local position

 

Share this post


Link to post
Share on other sites

Hi @enpu

Ok I see, that almost works but it's still seems to be off by some pixels - maybe because I'm centering my container.

I can easily work out how much it's off by, but I don't really understand why.

Thanks again for the tips!

Share this post


Link to post
Share on other sites

@enpu

I have a 768x768 sprite ('baseBoard') and it's anchor is in the center. I've placed some more 'baseBoard' sprites inside the main sprite to extend the board to the left and right.

The offset is exactly half of 768x768, so it looks like I should be keeping the anchor top left if I want to avoid it.

I pretty sure this is the reason, my logic on this was just a little flawed - thanks heaps for the help!

Share this post


Link to post
Share on other sites

Hi @enpu

I did notice something different, the sprite moved into the new container is in a slightly diff position.

I however still need to use the offset to make it work (I'm pretty sure this is my fault), but it's the same offset as before, which is strange.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.