Hearts
Click anywhere to create hearts
/**
* Copyright cobersky ( http://wonderfl.net/user/cobersky )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/qM0z
*/
package
{
import flash.display.Graphics;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
public class PointDemo_interpolate extends Sprite
{
private const _sp:Number=Math.PI*.1;
private const _p0:Point=new Point;
private const _pool:Vector.<Heart>=new Vector.<Heart>;
private var _r:Number;
private var _a:Number;
private var _t:Number=0;
private var _angel:Number=Math.PI*2;
public function PointDemo_interpolate()
{
stage.scaleMode=StageScaleMode.NO_SCALE;
stage.align=StageAlign.TOP_LEFT;
stage.frameRate=30;
stage.showDefaultContextMenu=false;
stage.addEventListener(MouseEvent.CLICK,onClick);
}
private function onClick(e:MouseEvent):void{
_p0.x=e.stageX;
_p0.y=e.stageY;
_angel=2*Math.PI;
_a=-Math.PI*Math.random()+Math.PI;
_r=Math.random()*100+100>>0;
_t=0;
while(_angel>0){
_angel-=_sp;
_t=_angel>Math.PI?(2-_angel/Math.PI):_angel/Math.PI;
const p1:Point=Point.polar(_r,_angel-_a);
p1.x+=_p0.x;
p1.y+=_p0.y;
createHeart(Point.interpolate(p1,_p0,_t),_angel/_sp);
}
}
private function createHeart(p:Point,n:uint):void{
const h:Heart=_pool.length?_pool.pop():new Heart();
h.x=p.x;
h.y=p.y;
h.n=n;
this.addChild(h);
h.addEventListener(Event.COMPLETE,push);
}
private function push(e:Event):void{
const h:Heart=e.target as Heart;
this.removeChild(h);
h.removeEventListener(Event.COMPLETE,push);
_pool.push(h)
}
}
}
import flash.display.Graphics;
import flash.display.Shape;
import flash.events.Event;
import flash.geom.Point;
class Heart extends Shape
{
private const _lifeTime:uint=10;
private var _v:Number;
public var n:uint;
public function Heart()
{
const a_sp:Number=0.05*Math.PI;
const angel:Number=Math.PI*.5;
const p:Point=new Point(0,2);
const g:Graphics=this.graphics;
g.lineStyle(1,0xff0000);
g.beginFill(0xff0000,0.4);
g.moveTo(p.x,p.y);
var a:Number=2*Math.PI;
var t:Number=0;
while(a>0){
a-=a_sp;
t=a>Math.PI?(2-a/Math.PI):a/Math.PI;
drawPoint(Point.interpolate(Point.polar(10,a-angel),p,t));
}
g.endFill();
this.scaleX=this.scaleY=0;
this.addEventListener(Event.ADDED_TO_STAGE,onAdded);
}
private function onAdded(e:Event):void{
this.rotation=Math.random()*180-90;
this._v=0;
this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
}
private function drawPoint(p:Point):void{
this.graphics.lineTo(p.x,p.y);
}
private function onEnterFrame(e:Event):void{
if(n>0){
n--;
}else{
this._v+=(1-this.scaleX)*.1;
this._v*=.9;
this.scaleX+=_v;
this.scaleY+=_v;
if(_v>-.001&&_v<.001){
this.scaleX=this.scaleY=1;
n=_lifeTime;
this.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
this.addEventListener(Event.ENTER_FRAME,dis);
}
}
}
private function dis(e:Event):void{
if(n>0){
n--;
}else{
this.scaleX-=0.1;
this.scaleY-=0.1;
if(this.scaleX<=0){
this.scaleX=this.scaleY=0;
this.removeEventListener(Event.ENTER_FRAME,dis);
this.dispatchEvent(new Event("complete"));
}
}
}
}