Timeline Manipulation


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

Class version of TimelineManipulation.


Copy this code and paste it in your HTML
  1. /**
  2.  * VERSION: Early
  3.  * DATE: 2011-11-15
  4.  * AS3
  5.  * TimelineTween, a class to simplify rollOver and rollOut event of a MovieClip based button.
  6.  **/
  7. package com.tl {
  8. import flash.events.MouseEvent;
  9. import flash.display.MovieClip;
  10. import flash.events.Event;
  11.  
  12. public class TimelineTween extends MovieClip{
  13.  
  14. public function TimelineTween(object:MovieClip) {
  15. object.addEventListener(MouseEvent.ROLL_OVER, onRollover);
  16. object.addEventListener(MouseEvent.ROLL_OUT, onRollout);
  17. }
  18.  
  19. /* Events */
  20. private function onRollover(me:MouseEvent):void{
  21. playTo(me.currentTarget, me.currentTarget.totalFrames);
  22. }
  23. private function onRollout(me:MouseEvent):void{
  24. rewindTo(me.currentTarget, 1);
  25. }
  26.  
  27. /* Public functions */
  28. public static function onRollAndOut(object:MovieClip):TimelineTween{
  29. return new TimelineTween(object);
  30. }
  31.  
  32. public static function playTo(object:MovieClip, frameNum:Number){
  33. object.types = "number";
  34. object.frameNum = frameNum;
  35. object.addEventListener(Event.ENTER_FRAME, playLoop);
  36. }
  37.  
  38. public static function rewindTo(object:MovieClip, frameNum:Number){
  39. object.types = "number";
  40. object.frameNum = frameNum;
  41. object.addEventListener(Event.ENTER_FRAME, rewindLoop);
  42. }
  43.  
  44. public static function rollToLabel(object:MovieClip, label:String):void{
  45. object.types = "label";
  46. object.frameLabel = label;
  47. object.addEventListener(Event.ENTER_FRAME, playLoop);
  48. }
  49.  
  50. public static function rewindToLabel(object:MovieClip, label:String):void{
  51. object.types = "label";
  52. object.frameLabel = label;
  53. object.addEventListener(Event.ENTER_FRAME, rewindLoop);
  54. }
  55.  
  56. /* Enterframe for mouse roll over event - start */
  57. private function playLoop(e:Event):void{
  58. if(e.currentTarget.types == "number"){
  59. if(e.currentTarget.currentFrame < e.currentTarget.frameNum){
  60. e.currentTarget.nextFrame();
  61. }else if(e.currentTarget.currentFrame == e.currentTarget.frameNum){
  62. e.currentTarget.removeEventListener(Event.ENTER_FRAME, playLoop);
  63. e.currentTarget.stop();
  64. }
  65. }else if(e.currentTarget.types == "label"){
  66. if(e.currentTarget.currentFrameLabel == e.currentTarget.frameLabel){
  67. e.currentTarget.removeEventListener(Event.ENTER_FRAME, playLoop);
  68. e.currentTarget.stop();
  69. }else{
  70. e.currentTarget.nextFrame();
  71. }
  72. }
  73. }
  74. /* Enterframe for mouse roll over event - end */
  75.  
  76.  
  77. /* Enterframe for mouse roll out event - start */
  78. private function rewindLoop(e:Event):void{
  79. if(e.currentTarget.types == "number"){
  80. if(e.currentTarget.currentFrame > e.currentTarget.frameNum){
  81. e.currentTarget.prevFrame();
  82. }else if(e.currentTarget.currentFrame == e.currentTarget.frameNum){
  83. e.currentTarget.removeEventListener(Event.ENTER_FRAME, rewindLoop);
  84. e.currentTarget.stop();
  85. }
  86. }else if(e.currentTarget.types == "label"){
  87. if(e.currentTarget.currentFrameLabel == e.currentTarget.frameLabel){
  88. e.currentTarget.removeEventListener(Event.ENTER_FRAME, rewindLoop);
  89. e.currentTarget.stop();
  90. }else{
  91. e.currentTarget.prevFrame();
  92. }
  93. }
  94. }
  95. /* Enterframe for mouse roll out event - end */
  96.  
  97. }
  98. }
  99.  
  100. /*
  101. How To use it.
  102.  
  103. Create one way classic tween animation and put stop(); in the first frame.
  104. Then, put this script outside your movie clip
  105.  
  106. // Standard use
  107. import com.tl.TimelineTween;
  108.  
  109. var movieClipButton:MovieClip = new MovieClip();
  110. TimelineTween.onRollAndOut(movieClipButton);
  111.  
  112. // Custom use
  113. import com.tl.TimelineTween;
  114.  
  115. var movieClipButton:MovieClip = new MovieClip();
  116. // Forward use
  117. TimelineTween.playTo(movieClipButton, 24); // playTo(object:MovieClip, frameNumber:Number);
  118. // Backward use
  119. TimelineTween.rewindTo(movieClipButton, 1); // rewindTo(object:MovieClip, frameNumber:Number);
  120. // Forward use with label
  121. TimelineTween.playTo(movieClipButton, "end_of_mc"); // playTo(object:MovieClip, label:String);
  122. // Backward use with label
  123. TimelineTween.rewindTo(movieClipButton, "beginning_of_mc"); // rewindTo(object:MovieClip, label:String);
  124.  
  125. */

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.