與先前的例子比較,這是一個漂亮的大工程,所以仔細看看代碼,玩玩最終生成的程序,之后我會詳細講解有關(guān)內(nèi)容。
代碼:
package { import flash.display.Shape; import flash.display.Sprite; import flash.events.Event; import flash.events.KeyboardEvent; import flash.events.MouseEvent; import flash.filters.GlowFilter; import flash.ui.Keyboard; /** * ... * @author bardpub */ [SWF(width=550,height=400,backgroundColor=0)] public class CopyingGraphics extends Sprite { private static const INIT_SEGMENTS:int = 10; private static const MAX_SEGMENTS:int = 20; private static const MIN_SEGMENTS:int = 3; private static const THICKNESS:int = 1; private static const COLOR:uint = 0x66CCCC; private static const ROTATION_RATE:int = 1; private var _shapeHolder:Sprite; private var _shapes:Vector.; public function CopyingGraphics() { init(); } private function init():void { _shapeHolder = new Sprite(); _shapeHolder.x = stage.stageWidth / 2; _shapeHolder.y = stage.stageHeight / 2; addChild(_shapeHolder); _shapes = new Vector.(); for (var i:int = 0; i < INIT_SEGMENTS; i++ ) { addSegment(); } positionSegments(); stage.addEventListener(MouseEvent.MOUSE_DOWN, onStageMouseDown); stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp); stage.addEventListener(KeyboardEvent.KEY_DOWN, onStageKeyDown); filters = [new GlowFilter(COLOR)]; } private function draw():void { var shape:Shape = _shapes[0]; shape.graphics.lineTo(shape.mouseX, shape.mouseY); var segments:int = _shapeHolder.numChildren; for (var i:int = 1; i < segments; i++ ) { _shapes[i].graphics.copyFrom(shape.graphics); } } private function addSegment():void { var shape:Shape = new Shape(); if (_shapes.length > 0) { shape.graphics.copyFrom(_shapes[0].graphics); } else { shape.graphics.lineStyle(THICKNESS, COLOR); } _shapes.push(shape); _shapeHolder.addChild(shape); } private function removeSegment():void { var shape:Shape = _shapes.pop(); _shapeHolder.removeChild(shape); } private function positionSegments():void { var segments:int = _shapeHolder.numChildren; var angle:Number = 360 / segments; for (var i:int = 1; i < segments; i++ ) { _shapes[i].rotation = angle * i; } } private function onStageMouseDown(event:MouseEvent):void { var shape:Shape = _shapes[0]; shape.graphics.moveTo(shape.mouseX, shape.mouseY); addEventListener(Event.ENTER_FRAME, onEnterFrame); } private function onStageMouseUp(event:MouseEvent):void { removeEventListener(Event.ENTER_FRAME, onEnterFrame); } private function onEnterFrame(event:Event):void { _shapeHolder.rotation += ROTATION_RATE; draw(); } private function onStageKeyDown(event:KeyboardEvent):void { switch (event.keyCode) { case Keyboard.UP: if (_shapeHolder.numChildren < MAX_SEGMENTS) { addSegment(); positionSegments(); } break; case Keyboard.DOWN: if (_shapeHolder.numChildren > MIN_SEGMENTS) { removeSegment(); positionSegments(); } break; } } } }
出處:藍色理想
責(zé)任編輯:bluehearts
上一頁 ActionScript3.0 Image Effects 復(fù)制圖形 [1] 下一頁 ActionScript3.0 Image Effects 復(fù)制圖形 [3]
◎進入論壇RIA設(shè)計與應(yīng)用版塊參加討論
|