AS3.0打造文档类纯代码时钟
作者:lazycat 日期:2010-05-06
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldType;
import flash.text.FontType;
import flash.text.TextFormatAlign;
[SWF(width = "465", height = "465", backgroundColor = "0xffffff", frameRate = "30")]
public class NoisyClock extends Sprite {
private const STAGE_WIDTH:Number = stage.stageWidth;
private const STAGE_HEIGHT:Number = stage.stageHeight;
private const STAGE_CENTER_X:Number = stage.stageWidth / 2;
private const STAGE_CENTER_Y:Number = stage.stageHeight / 2;
private const RADIAN_TO_DEGREE:Number = 180 / Math.PI;
private const DEGREE_TO_RADIAN:Number = Math.PI / 180;
private var clockMC:MovieClip
private var second_hand:Sprite;
private var minute_hand:Sprite;
private var hour_hand:Sprite;
private var center_pin:Sprite;
private var frame:Sprite;
private var frame_shadow:Sprite;
private var base_color:uint = 0x000000;
private var second_hand_shadow:Sprite;
private var minute_hand_shadow:Sprite;
private var hour_hand_shadow:Sprite;
private var center_pin_shadow:Sprite;
private var rSeconds:Number;
private var rMinutes:Number;
private var rHours:Number;
private var onmArray:Array = [];
public function NoisyClock() {
init();
}
private function init():void {
clock();
var now_date:Date = new Date();
second_hand.rotation = now_date.seconds * 6;
second_hand_shadow.rotation = now_date.seconds * 6;
rSeconds = now_date.seconds;
minute_hand.rotation = now_date.minutes * 6;
minute_hand_shadow.rotation = now_date.minutes * 6;
rMinutes = now_date.minutes;
hour_hand.rotation = now_date.hours * 30 + now_date.minutes/2;
hour_hand_shadow.rotation = now_date.hours * 30 + now_date.minutes / 2;
rHours = now_date.hours;
addEventListener(Event.ENTER_FRAME, main)
}
private function main(eventObject:Event):void {
var now_date:Date = new Date();
if ( rSeconds != now_date.seconds ) {
second_hand.rotation = now_date.seconds * 6;
second_hand_shadow.rotation = now_date.seconds * 6;
rSeconds = now_date.seconds;
}
if ( rSeconds == 1 && rMinutes == 0 && rHours != now_date.hours ) {
clockMC.x = Math.cos( ( hour_hand.rotation + 90 ) * DEGREE_TO_RADIAN ) * 480 + STAGE_CENTER_X;
clockMC.y = Math.sin( ( hour_hand.rotation + 90 ) * DEGREE_TO_RADIAN ) * 480 + STAGE_CENTER_Y;
clockMC.scaleX = clockMC.scaleY = 2.8;
} else if ( rSeconds == 2 && rMinutes == 0 && rHours != now_date.hours ) {
rHours = now_date.hours;
} else if ( rSeconds > 58 ) {
clockMC.x = Math.cos( ( minute_hand.rotation + 90 ) * DEGREE_TO_RADIAN ) * 280 + STAGE_CENTER_X;
clockMC.y = Math.sin( ( minute_hand.rotation + 90 ) * DEGREE_TO_RADIAN ) * 280 + STAGE_CENTER_Y;
clockMC.scaleX = clockMC.scaleY = 2.0;
} else if( rSeconds == 0 && rMinutes != now_date.minutes ) {
minute_hand.rotation = now_date.minutes * 6;
minute_hand_shadow.rotation = now_date.minutes * 6;
hour_hand.rotation = now_date.hours * 30 + now_date.minutes/2;
hour_hand_shadow.rotation = now_date.hours * 30 + now_date.minutes / 2;
rMinutes = now_date.minutes;
} else if( rSeconds > 2) {
clockMC.x = STAGE_CENTER_X;
clockMC.y = STAGE_CENTER_Y;
clockMC.scaleX = clockMC.scaleY = 1.0;
}
var i:int = onmArray.length
while ( i--) {
var t = onmArray[ i ]
t.vy += 0.10;
t.x += t.vx
t.y += t.vy;
t.rotation += t.rot;
if ( t.y > STAGE_HEIGHT + 0 ) {
onmArray.splice(i, 1);
clockMC.removeChild( t );
}
if ( Math.sqrt(t.x * t.x + t.y * t.y ) > 185 && t.reactFlag == false ) {
t.scaleX = t.scaleY *= 1.4;
t.reactFlag = true;
t.rot *= -1;
t.vy *= -0.4;
}
}
}
private function clock():void {
var st:ScreenTone = new ScreenTone()
clockMC = new MovieClip();
addChild( clockMC );
clockMC.x = STAGE_CENTER_X;
clockMC.y = STAGE_CENTER_Y;
frame_shadow = new Sprite();
frame_shadow.graphics.beginBitmapFill( st.st1 );
frame_shadow.graphics.lineStyle( 1, 0xffffff );
frame_shadow.graphics.drawCircle( 0, 0, 220);
frame_shadow.graphics.drawCircle( 0, 0, 190);
frame_shadow.graphics.endFill();
frame_shadow.x = 8;
frame_shadow.y = 8;
clockMC.addChild( frame_shadow );
frame = new Sprite();
frame.graphics.beginFill( 0xffffff );
frame.graphics.lineStyle( 1, base_color );
frame.graphics.drawCircle( 0, 0, 220);
frame.graphics.drawCircle( 0, 0, 190);
frame.graphics.endFill();
frame.x = 0;
frame.y = 0;
clockMC.addChild( frame );
//文本
var tf:TextFormat = new TextFormat();
tf.size = 38;
tf.align = TextFormatAlign.CENTER;
tf.bold = true
var txtArray:Array = []
for ( var i:uint = 1; i < 13; i++ ) {
var txt = new TextField();
txt.width = 42;
txt.height = 42;
var radius:int = 160;
var txtPos:Number = ( i * 30 + 180 ) * DEGREE_TO_RADIAN;
txt.x = Math.cos( txtPos ) * 0 - Math.sin( txtPos ) * radius - txt.width / 2;
txt.y = Math.cos( txtPos ) * radius + Math.sin( txtPos ) * 0 - txt.height / 2;
txt.type = TextFieldType.DYNAMIC;
txt.border = false;
txt.background = false;
txt.textColor = 0x666666;
txt.defaultTextFormat = tf;
txt.text = i;
clockMC.addChild( txt );
txtArray.push( txt );
}
//时针
hour_hand_shadow = new Sprite();
hour_hand_shadow.graphics.beginBitmapFill( st.st1 );
hour_hand_shadow.graphics.lineStyle( 1, 0xffffff );
hour_hand_shadow.graphics.drawRect( -4, 0, 8, -100 );
hour_hand_shadow.graphics.endFill();
hour_hand_shadow.x = 4;
hour_hand_shadow.y = 4;
clockMC.addChild( hour_hand_shadow );
//分针
minute_hand_shadow = new Sprite();
minute_hand_shadow.graphics.beginBitmapFill( st.st1 );
minute_hand_shadow.graphics.lineStyle( 1, 0xffffff );
minute_hand_shadow.graphics.drawRect( -2.5, 0, 5, -150 );
minute_hand_shadow.graphics.endFill();
minute_hand_shadow.x = 4;
minute_hand_shadow.y = 4;
clockMC.addChild( minute_hand_shadow );
hour_hand = new Sprite();
hour_hand.graphics.beginFill( 0x000000 );
hour_hand.graphics.lineStyle( 1, base_color );
hour_hand.graphics.drawRect( -4, 0, 8, -100 );
hour_hand.graphics.endFill();
hour_hand.x = 0;
hour_hand.y = 0;
clockMC.addChild( hour_hand );
minute_hand = new Sprite();
minute_hand.graphics.beginFill( 0x000000 );
minute_hand.graphics.lineStyle( 1, base_color );
minute_hand.graphics.drawRect( -2.5, 0, 5, -150 );
minute_hand.graphics.endFill();
minute_hand.x = 0;
minute_hand.y = 0;
clockMC.addChild( minute_hand );
second_hand_shadow = new Sprite();
second_hand_shadow.graphics.lineStyle( 4, base_color, 0.3 );
second_hand_shadow.graphics.moveTo( 0, 30 );
second_hand_shadow.graphics.lineTo( 0, -156 );
second_hand_shadow.x = 4;
second_hand_shadow.y = 4;
clockMC.addChild( second_hand_shadow );
second_hand = new Sprite();
second_hand.graphics.lineStyle( 2, 0xff0000 );
second_hand.graphics.moveTo( 0, 30 );
second_hand.graphics.lineTo( 0, -156 );
second_hand.x = 0;
second_hand.y = 0;
clockMC.addChild( second_hand );
center_pin_shadow = new Sprite();
center_pin_shadow.graphics.beginFill( 0x000000 );
center_pin_shadow.graphics.lineStyle( 1, 0xffffff );
center_pin_shadow.graphics.drawCircle( 0, 0, 8 );
center_pin_shadow.graphics.endFill();
center_pin_shadow.x = 4;
center_pin_shadow.y = 4;
center_pin_shadow.alpha = 0.3;
clockMC.addChild( center_pin_shadow );
center_pin = new Sprite();
center_pin.graphics.beginFill( 0xff0000 );
center_pin.graphics.lineStyle( 1, 0xff0000 );
center_pin.graphics.drawCircle( 0, 0, 8 );
center_pin.graphics.endFill();
center_pin.x = 0;
center_pin.y = 0;
clockMC.addChild( center_pin );
}
}
}
import flash.display.Sprite;
import flash.display.BitmapData;
class ScreenTone extends Sprite
{
public var st0:BitmapData;
public var st1:BitmapData;
public var st2:BitmapData;
public function ScreenTone():void {
var base_color:uint = 0x000000;
st0 = new BitmapData( 4, 4, false, 0x00ffffff);
var ptn =
[[1, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 0]]
var bitmapW:int = ptn[0].length;
var bitmapH:int = ptn.length;
for (var yy:int = 0; yy < bitmapH; yy++) {
for (var xx:int = 0; xx < bitmapW; xx++) {
if ( ptn[yy][xx] == 1 ) {
st0.setPixel32( xx, yy, base_color );
}
}
}
st1 = new BitmapData( 2, 2, false, 0x00ffffff);
ptn =
[[1, 0],
[0, 1]]
bitmapW = ptn[0].length;
bitmapH = ptn.length;
for (yy = 0; yy < bitmapH; yy++) {
for (xx = 0; xx < bitmapW; xx++) {
if ( ptn[yy][xx] == 1 ) {
st1.setPixel32( xx, yy, base_color );
}
}
}
st2 = new BitmapData( 6, 6, false, 0x00ffffff);
ptn =
[[1, 0, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[1, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 1],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 1, 0, 1]]
bitmapW = ptn[0].length;
bitmapH = ptn.length;
for (yy = 0; yy < bitmapH; yy++) {
for (xx = 0; xx < bitmapW; xx++) {
if ( ptn[yy][xx] == 1 ) {
st2.setPixel32( xx, yy, base_color );
}
}
}
}
}