Jump to content

Child position relative to parent anchor strangeness..


moszis
 Share

Recommended Posts

 I have a sprite with one child text object.   Originally, I didn't feel the need to use DisplayObject as all I really need is to display a number at a top left corner of a sprite.  

 

 When anchor of parent sprite is 0, 0, all is working as intended.   Text appears in the top left corner.   However, this is a dragable sprite, so during drag I change the anchor of it to 0.5, 0.5(so the mouse appears in the middle).   As expected, child text object jumps to the middle of parent sprite.  

 

 Now, I thought I would simply adjust x,y coordinates or anchor of text object during drag of parent object, but I'm experiencing some very strange behavior.    I cant seem to figure out the pattern here.   For x,y coordinates of text object I need to use some very strange numbers that are in no way relative to the parent sprite.     Also it seems that this arbitrary number changes with the original quality of the image used for sprite, even though this sprite is sized into static width/height during display.  

 

 

Thank you for your help in advance

Link to comment
Share on other sites

Quick update..

 

 It seems that when sprite is not sized down, everything works as I would expect.   Top left corner is simply - (image size * anchor).   So it looks like child object coordinates are based on parents original image size and not relative to what is being displayed.   I'm pretty sure that i'm doing something wrong, as this is a problem many would have ran in to by now.

Link to comment
Share on other sites

 

 Now, I thought I would simply adjust x,y coordinates or anchor of text object during drag of parent object, but I'm experiencing some very strange behavior.   

 

what is your calculation on this?

when/where are you adjusting the position?

I suspect from your follow up post that the scale of your child sprite is inheriting the parent scaling... which is to be expected.. your positioning assignment operating probably doesn't account for it.. and it may even be applied prior to the scaling.. 

Link to comment
Share on other sites

Hey mrBRC,

 

   Thank you for your response.  

 

   I did not consider child scaling with parent scaling by default.   I have a piece of code that automatically scales the child, so I didn't even notice that it does by default.   Its actually very helpful to know as I will clean things up a bit.

 

   I solved the issue above by using getLocalBounds() of parent Sprite.  Which gives the "unscaled" width/height of sprite image, which for some reason child object needs to appropriately offset the anchor of parent. 

 

   The calculation is very simple:  top left corner of parent   =  negative (parent.getLocalBounds().width * parent.anchor.x).   And same for height.  

 

   I am still not clear though why child needs the parents "unscaled" size to properly offset.   For example if original image size of parent is 500 by 500 but I adjust height/width to display as 100 x 100.   I will need to offset the 0.5 anchor by 250x and 250y, instead of 50x and 50y as I originally thought.   Even if child size is scaled down by 5x together with parent, it still doesn't change the fact that parent now only has 50 pixels from the center to the edge..

Link to comment
Share on other sites

oh.. that's because the anchor isn't relative to the parent.. it's relative to x,y positioning of the sprite itself.

 

if you want the sprite to be centered to the parent, leave the sprites anchor at 0.5,0.5 (centering the sprite texture and boundingbox to it's own x,y position) and adjust only the sprites positioning relative to it's parent.

the anchor of the parent really should be at 0,0 because it's really only being used as a DisplayObjectContainer (which doesn't have a anchor property) neither does DisplayObject.. because neither of those objects have baseTextures/Textures associated to them. 

 

the sprite.scale property/member is inherited from DisplayObject

the DisplayObjectContainer.scale property/member is also inherited from DisplayObject

 

when you say you're sprite is drag-able.. do you mean resize-able.. as in dragging the corners and/or bounding edges?

Link to comment
Share on other sites

By dragable I mean user is able to click on it and drag it.   This is why I need to have an anchor at the middle.  

 

Thank you for pointing out scale property!  Allows me to do the same thing I did with getLocalBounds but in much cleaner way.

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