flash on 2009-9-7
/**
* Copyright harmonic ( http://wonderfl.net/user/harmonic )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/n16l
*/
package {
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Rectangle;
import flash.utils.Timer;
import net.hires.debug.Stats;
[SWF(backgroundColor=0x0, frameRate=60)]
public class FlashTest extends Sprite {
private const WIDTH:int = 465;
private const HEIGHT:int = 465;
private const MARGIN:int = 5;
private var _dots:Vector.<Dot>;
private var _circle:Sprite;
private var _canvas:BitmapData;
private var _rectangle:Rectangle;
private var _timer:Timer;
public function FlashTest() {
// write as3 code here..
_dots = new Vector.<Dot>;
_canvas = new BitmapData( WIDTH, HEIGHT , false, 0x0);
addChild( new Bitmap( _canvas ) );
_rectangle = new Rectangle( 0, 0, WIDTH, HEIGHT );
_timer = new Timer( 100 , 1 );
//---------------------------------------
_circle = new Sprite();
_circle.graphics.beginFill( 0xFFFFFF , 0.0 );
_circle.graphics.drawCircle( 0, 0, 100 );
_circle.graphics.endFill();
addChild( _circle );
//---------------------------------------
var cols:int = Math.ceil( WIDTH / MARGIN );
var rows:int = Math.ceil( HEIGHT / MARGIN );
var counter:int = 0;
for( var i:int = 0 ; i < rows ; i++ )
{
for( var j:int = 0 ; j < cols ; j++ )
{
var dot:Dot = new Dot();
dot.x = j * MARGIN;
dot.y = i * MARGIN;
_dots.push( dot );
counter ++;
}
}
addChild( new Stats() );
stage.addEventListener( MouseEvent.MOUSE_MOVE , onMouseMoveHandler );
addEventListener( Event.ENTER_FRAME , _onEnterFrame );
}
private function onMouseMoveHandler( event:MouseEvent ):void {
_timer.reset();
_timer.start();
}
//
private function _onEnterFrame( event:Event ):void {
_circle.x += ( stage.mouseX - _circle.x ) * 0.1;
_circle.y += ( stage.mouseY - _circle.y ) * 0.1;
_canvas.lock();
_canvas.fillRect( _rectangle , 0x000000 );
var radius:Number = _circle.width * 0.5;
var minX:Number = _circle.x - radius;
var maxX:Number = _circle.x + radius;
var minY:Number = _circle.y - radius;
var maxY:Number = _circle.y + radius;
var n:int = _dots.length;
var dot:Dot;
for( var i:int = 0 ; i< n ; i++ )
{
dot = _dots[ i ];
dot.y -= 8;
if( dot.y < 0 ){
dot.y += HEIGHT;
}
if( dot.x < minX ){ dotDeactivate( dot ); continue; }
if( dot.x > maxX ){ dotDeactivate( dot ); continue; }
if( dot.y < minY ){ dotDeactivate( dot ); continue; }
if( dot.y > maxY ){ dotDeactivate( dot ); continue; }
if( myHitTest( dot ) ){
dotActivate( dot );
}else{
dotDeactivate( dot );
}
_canvas.unlock();
}
}
private function myHitTest( target:Dot ):Boolean {
var result:Boolean = false;
var radius:Number = _circle.width * 0.5;
var distaceX:Number = ( _circle.x - target.x ) * ( _circle.x - target.x );
var distaceY:Number = ( _circle.y - target.y ) * ( _circle.y - target.y );
var distance:Number = Math.sqrt( distaceX + distaceY );
if( distance < radius ){
result = true;
}
return result;
}
private function dotActivate( target:Dot ):void {
var radian:Number = Math.atan2( _circle.x - target.x , _circle.y - target.y );
var radius:Number = _circle.width * Math.random() * 0.5;
target.gx += ( -Math.sin( radian ) * radius - target.gx ) * 0.1;
target.gy += ( -Math.cos( radian ) * radius - target.gy ) * 0.1;
_canvas.setPixel32( target.x + target.gx , target.y + target.gy , 0xFFFFFF );
}
private function dotDeactivate( target:Dot ):void {
target.gx -= target.gx * 0.05;
target.gy -= target.gy * 0.05;
if( target.gx != 0 && target.gy != 0 ){
_canvas.setPixel32( target.x + target.gx , target.y + target.gy , 0xFFFFFF );
}
}
}
}
class Dot {
public var x:int;
public var y:int;
public var gx:int;
public var gy:int;
public function Dot()
{
x = 0;
y = 0;
gx = 0;
gy = 0;
}
}