TreeClock 10_4_24
...
@author Kevin_Yin
/**
* Copyright Kevin_Yin ( http://wonderfl.net/user/Kevin_Yin )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/tACK
*/
package
{
import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.utils.Timer;
/**
* ...
* @author Kevin_Yin
*/
[SWF(width="465", height="465", backgroundColor="0xb0b0b0")]
public class TreeClock extends Sprite
{
private const LEVEL:uint = 6;
private const BRANCH_COUNT:uint = 3;
private var _hour:Number;
private var _minute:Number;
private var _second:Number;
private var timer:Timer = new Timer(1000);
private var _levelCount:uint = LEVEL;
private var _rotations:Array = new Array();
private var _lengthRates:Array = [.6, .6, .6];
private var _alphaRates:Array = [.6, .6, .6];
private var _lengths:Array = [60, 100, 160];
private var _thicknesses:Array = [4, 2, 2];
private var _colors:Array = [0x000000, 0x000000, 0xff0000];
private var _currentRotation:Number = 0;
private var _currentX:Number;
private var _currentY:Number;
public function TreeClock():void
{
var date:Date = new Date();
_hour = date.getHours() % 12;
_minute = date.getMinutes();
_second = date.getSeconds();
timer.addEventListener(TimerEvent.TIMER, timeUpdate);
timer.start();
}
private function resetCurrentPoint():void
{
_currentRotation = 0;
_currentX = stage.stageWidth * .5;
_currentY = stage.stageHeight * .5;
graphics.moveTo(_currentX, _currentY);
}
private function timeUpdate(e:TimerEvent):void
{
_second++;
if (_hour % 12 == 0)
{
_hour = 0;
}
if (_minute >= 60)
{
_minute = 0;
_hour++;
}
if (_second >= 60)
{
_second = 0;
_minute++;
}
updateRotationsByTime();
graphics.clear();
resetCurrentPoint();
branch(LEVEL);
}
private function branch(level:uint):void
{
if (level <= 0) return;
for (var i:int = 0; i < BRANCH_COUNT; i++)
{
_currentRotation += _rotations[i];
_currentX += _lengths[i] * Math.cos(_currentRotation) * Math.pow(_lengthRates[i], LEVEL - level);
_currentY += _lengths[i] * Math.sin(_currentRotation) * Math.pow(_lengthRates[i], LEVEL - level);
graphics.lineStyle(_thicknesses[i], _colors[i], Math.pow(_alphaRates[i], LEVEL - level));
graphics.lineTo(_currentX, _currentY);
branch(level - 1);
_currentX -= _lengths[i] * Math.cos(_currentRotation) * Math.pow(_lengthRates[i], LEVEL - level);
_currentY -= _lengths[i] * Math.sin(_currentRotation) * Math.pow(_lengthRates[i], LEVEL - level);
_currentRotation -= _rotations[i];
graphics.moveTo(_currentX, _currentY);
}
}
private function updateRotationsByTime():void
{
_rotations = [Math.PI * 2 / 12 * _hour - Math.PI *.5,
Math.PI * 2 / 60 * _minute - Math.PI *.5,
Math.PI * 2 / 60 * _second - Math.PI *.5];
}
}
}