Jump to content

Can't figure out how to move sprite A on top of sprite B


Rumthorp
 Share

Recommended Posts

I'm really struggling with getting one sprite to sit on top of another sprite. Each sprite is in a separate container. I've read I'm supposed to use toLocal and toGlobal methods but I can't find any example of those methods being used in this way. I've tried using these methods as described but the sprite positions never match up. The closest I've come to is this:
 

spriteB.position = spriteB.toLocal(spriteA.toGlobal(root.stage.position));

That makes the y axis match but the x is way off.

Any help would be appreciated. I've been at this for a while.

Link to comment
Share on other sites

You could do something like this:
 

// Get the global position of 0,0 (origo of spriteA) in spriteA:s coordinate.
const aGlobal = this.spriteA.toGlobal(new Point(0,0));
// Transform that coordinate into the containers (where spriteB is) coordinate system.
const aInBContainer = this.spriteB.parent.toLocal(aGlobal);
// Set the sprite B position to that coordinate.
this.spriteB.position = aInBContainer;

 

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.

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

  • Recently Browsing   0 members

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