switch parent while remaining position


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function switchDisplayObjectContainer(obj:DisplayObject, oldContainer:DisplayObjectContainer, newContainer:DisplayObjectContainer):void {
  2. var startLocalPos:Point = new Point(obj.x, obj.y);
  3. var globalPos:Point = oldContainer.localToGlobal(startLocalPos);
  4.  
  5. newContainer.addChild(obj);
  6.  
  7. var newLocalPos:Point = newContainer.globalToLocal(globalPos);
  8. obj.x = newLocalPos.x;
  9. obj.y = newLocalPos.y;
  10. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.