Snake Drag and throw a ball, or simply click a ball. miyaoka forked:0 favorite:0 lines:162 license : MIT License modified : 2009-05-23 20:21:28 /* Drag and throw a ball, or simply click a ball. */ package { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; [SWF(width = "465", height = "465", backgroundColor = 0x0, frameRate = "60")] public class Snake extends Sprite { private var movers:Array = new Array(); public function Snake():void { for (var j:int = 0; j < 3; j++) { var moverContainer:Sprite = new Sprite(); moverContainer.y = 445; addChild(moverContainer); var moverX:Number = Math.random() * 300 + 100; var moverY:Number = Math.random() * -300 - 100; for (var i:int = 0; i < 100; i++) { var mover:Mover = new Mover(); mover.x = moverX; mover.y = moverY; moverContainer.addChild(mover); mover.addEventListener("throw", throwHandler); } } } private function throwHandler(e:Event):void { var mover:Mover = Mover(e.target); Sprite(mover.parent).setChildIndex(mover, 0); for (var j:int = 0; j < numChildren; j++) { var moverContainer:Sprite = Sprite(getChildAt(j)); var m0:Mover = Mover(moverContainer.getChildAt(0)); for (var i:int = 0; i < moverContainer.numChildren; i++) { var m:Mover = Mover(moverContainer.getChildAt(i)); m.addVector(mover.throwVect, m0.x, m0.y, 10*i); } } } } } import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.geom.Point; import flash.utils.Timer; import flash.events.TimerEvent; import flash.filters.GlowFilter; import flash.filters.DropShadowFilter; class Mover extends Sprite { private static var grav:Number = 0.5; private static var w:Number = 465; private static var yRef:Number = 0.85; private static var xRef:Number = 0.75; private var yy:Number = 0; private var xx:Number = 0; private var pts:Array = new Array(); private var isDragged:Boolean = false; private var lastPt:Point; public var throwVect:Point; private var timer:Timer = new Timer(1); public function Mover():void { addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event):void { removeEventListener(Event.ADDED_TO_STAGE, init); buttonMode = true; graphics.beginFill(0xFFFFFF, 1); graphics.drawCircle( 0, 0, Math.random()*20+1); startMove(); lastPt = new Point(x, y); filters = [ new DropShadowFilter(0, 0, 0xFFFFFF, 1, 40, 40, 1, 3) ]; addEventListener(MouseEvent.MOUSE_DOWN, function ():void { startDrag(); isDragged = true; pts = []; addEventListener(Event.ENTER_FRAME, recPt, false, 0, true); stopMove(); }); stage.addEventListener(MouseEvent.MOUSE_UP, function ():void { if (!isDragged) return; isDragged = false; stopDrag(); removeEventListener(Event.ENTER_FRAME, recPt); throwVect = new Point(x, y).subtract(lastPt); if (throwVect.x == 0) { throwVect.x = Math.random() * 60 - 30; throwVect.y = Math.random() * -10 - 15; } dispatchEvent(new Event("throw")); startMove(); }); } private function recPt(e:Event):void { lastPt = new Point(x, y); } public function addVector(pt:Point, x_:Number, y_:Number, delay:Number):void { timer.reset(); timer = new Timer(delay, 1); timer.start(); timer.addEventListener(TimerEvent.TIMER_COMPLETE, function ():void { startMove(); x = x_; y = y_; yy = pt.y; xx = pt.x; }); } private function startMove():void { addEventListener(Event.ENTER_FRAME, enterFrameHandler, false, 0, true); } private function stopMove():void { removeEventListener(Event.ENTER_FRAME, enterFrameHandler); } private function enterFrameHandler(e:Event):void { yy += grav; y += yy; x += xx; if (y > 0 ) { y *= -yRef; yy *= -yRef; if (yy > -4) { y = 0; yy = 0; } } if (x < 0 ) { x *= -xRef; xx *= -xRef; } else if (x > w) { x = w - (x - w) * xRef; xx *= -xRef; } if (y == 0 && yy == 0) { xx *= 0.98; if (Math.abs(xx) < 1) stopMove(); } } } Code Fullscreen Preview Fullscreen TimerEvent.ENTER_FRAME TimerEvent.ADDED_TO_STAGE delay removeEventListener addEventListener setChildIndex target startDrag stopDrag subtract getChildAt numChildren dispatchEvent buttonMode DropShadowFilter parent filters Point start Math.abs