Posted By

liuran on 06/14/09


Tagged

flash preloader as3


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

sweetbabyyoung
Pingpongbal
iamgowan
tspitzr
dubogii
fedecp


Flash Preloader as3 code like apple style.


 / Published in: ActionScript 3
 

URL: http://www.stevensacks.net/2008/10/01/as3-apple-style-preloader/

AS3 Apple Style Preloader

  1. package net.stevensacks.preloaders
  2. {
  3. import flash.events.TimerEvent;
  4. import flash.events.Event;
  5. import flash.display.Sprite;
  6. import flash.display.Shape;
  7. import flash.utils.Timer;
  8.  
  9. public class CircleSlicePreloader extends Sprite
  10. {
  11. private var timer:Timer;
  12. private var slices:int;
  13. private var radius:int;
  14.  
  15. public function CircleSlicePreloader(slices:int = 12, radius:int = 6)
  16. {
  17. super();
  18. this.slices = slices;
  19. this.radius = radius;
  20. draw();
  21. addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
  22. }
  23. private function onAddedToStage(event:Event):void
  24. {
  25. removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
  26. addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
  27. timer = new Timer(65);
  28. timer.addEventListener(TimerEvent.TIMER, onTimer, false, 0, true);
  29. timer.start();
  30. }
  31. private function onRemovedFromStage(event:Event):void
  32. {
  33. removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
  34. addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
  35. timer.reset();
  36. timer.removeEventListener(TimerEvent.TIMER, onTimer);
  37. timer = null;
  38. }
  39. private function onTimer(event:TimerEvent):void
  40. {
  41. rotation = (rotation + (360 / slices)) % 360;
  42. }
  43. private function draw():void
  44. {
  45. var i:int = slices;
  46. var degrees:int = 360 / slices;
  47. while (i--)
  48. {
  49. var slice:Shape = getSlice();
  50. slice.alpha = Math.max(0.2, 1 - (0.1 * i));
  51. var radianAngle:Number = (degrees * i) * Math.PI / 180;
  52. slice.rotation = -degrees * i;
  53. slice.x = Math.sin(radianAngle) * radius;
  54. slice.y = Math.cos(radianAngle) * radius;
  55. addChild(slice);
  56. }
  57. }
  58. private function getSlice():Shape
  59. {
  60. var slice:Shape = new Shape();
  61. slice.graphics.beginFill(0x666666);
  62. slice.graphics.drawRoundRect(-1, 0, 2, 6, 12, 12);
  63. slice.graphics.endFill();
  64. return slice;
  65. }
  66. }
  67. }

Report this snippet  

You need to login to post a comment.