AS3: Align objects on Stage utility


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

The way you use this is: Align.toTopCenter( objectOnStage );


Copy this code and paste it in your HTML
  1. package utils
  2. {
  3. import flash.display.DisplayObject;
  4.  
  5. public class Align
  6. {
  7.  
  8. public function Align()
  9. {
  10.  
  11. }
  12.  
  13. public static function toCenter( obj:DisplayObject ):void
  14. {
  15. obj.x = obj.stage.stageWidth / 2 - obj.width / 2;
  16. obj.y = obj.stage.stageHeight / 2 - obj.height / 2;
  17. }
  18.  
  19. public static function toTopLeft( obj:DisplayObject ):void
  20. {
  21. obj.x = obj.stage.x;
  22. obj.y = obj.stage.y;
  23. }
  24.  
  25. public static function toTopCenter( obj:DisplayObject ):void
  26. {
  27. obj.x = obj.stage.stageWidth / 2 - obj.width / 2;
  28. obj.y = obj.stage.y;
  29. }
  30.  
  31. public static function toTopRight( obj:DisplayObject ):void
  32. {
  33. obj.x = obj.stage.stageWidth - obj.width;
  34. obj.y = obj.stage.y;
  35. }
  36.  
  37. public static function toBottomRight( obj:DisplayObject ):void
  38. {
  39. obj.x = obj.stage.stageWidth - obj.width;
  40. obj.y = obj.stage.stageHeight - obj.height;
  41. }
  42.  
  43. public static function toBottomCenter( obj:DisplayObject ):void
  44. {
  45. obj.x = obj.stage.stageWidth / 2 - obj.width / 2;
  46. obj.y = obj.stage.stageHeight - obj.height;
  47. }
  48.  
  49. public static function toBottomLeft( obj:DisplayObject ):void
  50. {
  51. obj.x = obj.stage.x;
  52. obj.y = obj.stage.stageHeight - obj.height;
  53. }
  54. }
  55. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.