Toggle animation

by Fumio
♥2 | Line 39 | Modified 2010-08-30 22:50:01 | MIT License
play

ActionScript3 source code

/**
 * Copyright Fumio ( http://wonderfl.net/user/Fumio )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/yMRk
 */

package {
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.display.Graphics;
	import flash.display.GradientType;
	import flash.events.MouseEvent;
	import flash.display.Sprite;

	public class RotateMC extends MovieClip {
		// private var isRotate:Boolean;
		private var mySprite:Sprite;
		public function RotateMC() {
			// isRotate = false;
			mySprite = createRectangle();
			mySprite.addEventListener(MouseEvent.CLICK, startRotate);
		}
		/*
		public function changeMode():void {
			if (! isRotate) {
				addEventListener(Event.ENTER_FRAME, doRotate);
				isRotate = true;
			} else {
				removeEventListener(Event.ENTER_FRAME, doRotate);
				isRotate = false;
			}
		}
		*/
		private function startRotate(eventObject:MouseEvent):void {
			mySprite.removeEventListener(MouseEvent.CLICK, startRotate);
			mySprite.addEventListener(MouseEvent.CLICK, stopRotate);
			addEventListener(Event.ENTER_FRAME, doRotate);
		}
		private function stopRotate(eventObject:MouseEvent):void {
			mySprite.removeEventListener(MouseEvent.CLICK, stopRotate);
			mySprite.addEventListener(MouseEvent.CLICK, startRotate);
			removeEventListener(Event.ENTER_FRAME, doRotate);
		}
		private function doRotate(evt:Event):void {
			// rotation +=  2;
			mySprite.rotation +=  2;
		}
		private function createRectangle():Sprite {
			var size:Number = 150;
			var _sprite:Sprite = new Sprite();
			var myGraphics:Graphics = _sprite.graphics;
			addChild(_sprite);
			myGraphics.beginGradientFill(GradientType.LINEAR, [0x00FF00, 0x0000FF], [1, 1], [0, 255]);
			myGraphics.drawRect(-size / 2, -size / 2, size, size);
			_sprite.x = stage.stageWidth / 2;
			_sprite.y = stage.stageHeight / 2;
			return _sprite;
		}
	}
}