Breaking Sprite
/**
* Copyright ericyang ( http://wonderfl.net/user/ericyang )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/qUJx
*/
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class FlashTest extends Sprite {
public function FlashTest() {
// write as3 code here..
var spContainer:MovieClip = new MovieClip();
spContainer.graphics.beginFill(0x0000ff,0.3);
spContainer.graphics.drawRect(0,0,800,600);
spContainer.graphics.endFill();
addChild(spContainer);
var sp:Sprite = new Sprite();
sp.graphics.beginFill(0xff00ff);
sp.graphics.drawRect(0,0,100,30);
sp.graphics.endFill();
spContainer.addChild(sp);
var text:TextField = new TextField();
text.text = "BREAK";
var sp2:Sprite = new Sprite();
sp2.graphics.beginFill(0xff00,0.5);
sp2.graphics.drawRect(0,0,100,30);
sp2.graphics.endFill();
spContainer.addChild(sp2);
sp2.x = 200;
sp2.addChild(text);
sp2.addEventListener(MouseEvent.CLICK, function():void{
text.text = "explode";
spPixel.explode();
}); //addChild(text);
sp2.buttonMode = true;
sp.y = 100;
var spPixel:pixelator = new pixelator(sp);
spContainer.addChild(spPixel);
// spPixel.y = 100;
}
}
}
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.display.PixelSnapping;
/* Takes a bitmap and attaches it to a sprite */
class pixel extends Sprite {
public var origX:int;
public var origY:int;
public var tox:Number;
public var toy:Number;
public var xcount:Number = 0;
public var ycount:Number = 0;
public var step:Number = Math.random() * 0.5;
public var rad:Number = Math.round(Math.random() * 5);
public function pixel(_bitmapData:BitmapData, _origX:int, _origY:int) {
addChild(new Bitmap(_bitmapData, PixelSnapping.ALWAYS));
origX = _origX;
origY = _origY;
}
}
/* erikhallander@gmail.com */
import flash.display.*;
import flash.utils.setTimeout;
import flash.utils.ByteArray;
import flash.events.*;
import flash.geom.Rectangle;
import flash.geom.Matrix;
//import com.pixel;
import gs.TweenMax
import gs.easing.*;
import flash.filters.GlowFilter;
import flash.filters.BlurFilter;
import fl.transitions.easing.None;
import fl.transitions.easing.Strong;
class pixelator extends Sprite {
private var target:MovieClip;
private var orgClip:Sprite;
private var orgSquare:Rectangle;
private var orgBMD:BitmapData;
private var numPixels:int;
private var pixelBitmap:BitmapData;
private var blur:BlurFilter = new BlurFilter(1,1,3);
private var glow:GlowFilter = new GlowFilter(0xFFFF99,1,10,10,10,2);
private var colors:Array = [0xFFFF99,0xFFCC33,0xFF9900,0xFF3333];
public var pixelExplosion:* = this;
public var doneCreating:Boolean = false;
/* Const */
public function pixelator(_orgClip:Sprite) {
orgClip = _orgClip;
target = MovieClip(orgClip.parent);
orgSquare = orgClip.getBounds(orgClip);
x = orgClip.x+orgSquare.x;
y = orgClip.y+orgSquare.y;
setupPixels();
}
public function explode():void {
orgClip.visible = false;
pixelExplosion.visible = true;
var s:int = setTimeout(theVoid,675,pixelExplosion);
var rect:Rectangle = new Rectangle(0, 0, orgClip.width, orgClip.height);
var pixel_array:ByteArray = orgBMD.getPixels(rect);
for(var i:int = 0; i<numPixels; i++) {
var cPixel:pixel = pixel(getChildAt(i));
cPixel.visible = true;
TweenMax.to(cPixel,.7,{x:cPixel.tox,y:cPixel.toy,ease:None.easeOut});
}
}
private function theVoid(m:*):void {
dispatchEvent(new Event("ANIM_DONE"));
m.addEventListener(Event.ENTER_FRAME,myriad);
};
private function myriad(e:Event):void {
var rnd:int = 6;
for(var i:int = 0; i<numPixels; i++) {
var cPixel:pixel = pixel(getChildAt(i));
cPixel.y += Math.sin(cPixel.ycount)*(cPixel.rad*1.5);
cPixel.x += Math.cos(cPixel.xcount)*(cPixel.rad*0.7);
cPixel.xcount += cPixel.step *1.5;
cPixel.ycount += cPixel.step * .5;
};
};
public function retract():void {
var s:int = setTimeout(completeHandler,490);
pixelExplosion.removeEventListener(Event.ENTER_FRAME,myriad);
for(var i:int = 0; i<numPixels; i++) {
var cPixel:pixel = pixel(getChildAt(i));
cPixel.visible = true;
//TweenMax.to(cPixel,.5,{y:cPixel.origY,x:cPixel.origX,ease:Strong.easeIn,onComplete:function(){cPixel.filters = [glow,blur];cPixel.visible = false;}});
};
function completeHandler():void {
orgClip.visible = true;
pixelExplosion.visible = false;
dispatchEvent(new Event("PIXELS_DONE"));
}
}
private function setupPixels():void {
var myMatrix:Matrix = new Matrix();
myMatrix.tx = -orgSquare.x;
myMatrix.ty = -orgSquare.y;
orgBMD = new BitmapData(orgClip.width, orgClip.height, true, 0xFF000000);
orgBMD.draw(orgClip, myMatrix);
numPixels = 0;
var rect:Rectangle = new Rectangle(0, 0, orgClip.width, orgClip.height);
var pixel_array:ByteArray = orgBMD.getPixels(rect);
for(var i:int = 0; i<orgClip.height; i+=2) {
for(var j:int = 0; j<orgClip.width; j+=2) {
var angle:Number = Math.random()*360;
var radius:Number = Math.random()*80;
if(orgBMD.getPixel(j, i)>0) {
var cPixel:pixel = new pixel(new BitmapData(2, 2, false, orgBMD.getPixel(j, i)), j, i);
cPixel.x = j;
cPixel.y = i;
cPixel.tox = Math.round(j+Math.cos(angle)*radius);
cPixel.toy = Math.round(i+Math.sin(angle)*radius);
cPixel.visible = false;
glow.color = orgBMD.getPixel(j, i);//colors[Math.round(Math.random()*(colors.length-1))];
cPixel.filters = [glow,blur];
addChild(cPixel);
numPixels++;
}
}
}
doneCreating = true;
}
}