与先前的例子比较,这是一个漂亮的大工程,所以仔细看看代码,玩玩最终生成的程序,之后我会详细讲解有关内容。
代码:
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; } } } }
出处:蓝色理想
责任编辑:bluehearts
上一页 ActionScript3.0 Image Effects 复制图形 [1] 下一页 ActionScript3.0 Image Effects 复制图形 [3]
◎进入论坛RIA设计与应用版块参加讨论
|