Spew
Updated Version of Rainbow BlockBreaker with some extra functionality
/**
* Copyright scottman.gibbs ( http://wonderfl.net/user/scottman.gibbs )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/ws5D
*/
// forked from scottman.gibbs's Rainbow Breaker 2.0
// Updated Version of Rainbow BlockBreaker with some extra functionality
package
{
import flash.utils.clearInterval;
import flash.utils.setInterval;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import net.hires.debug.Stats;
[SWF(width = "800", height = "500", frameRate = "30")]
public class BlockBreaker extends Sprite
{
private static const HEIGHT:Number = 500;
private static const WIDTH:Number = 800;
private var _canvas:BitmapData;
private var _balls:Vector.<Particle>;
private var _bar:Bitmap;
private static const SPEED:Number = 7;
private var ID:Number;
public function BlockBreaker()
{
_canvas = new BitmapData(WIDTH, HEIGHT,false,0x000000);
addChild(new Bitmap(_canvas));
var b:BitmapData = new BitmapData(50, 10, false, 0x00FF00);
addChild(_bar = new Bitmap(b));
_bar.y = HEIGHT - 50;
var _ball:Particle = new Particle(WIDTH/2, HEIGHT/2);
_ball.vx = Math.random()*(2*Math.round(Math.random())-1)*SPEED;
_ball.vy = -Math.random() *SPEED -1;
_ball.color = Math.floor(Math.random()*0xFFFFFF);
_balls = new Vector.<Particle>();
_balls.push(_ball);
stage.addEventListener(MouseEvent.CLICK, mouseClick);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
addEventListener(Event.ENTER_FRAME, update);
}
private function mouseDown(e:MouseEvent):void {
mouseSpray();
ID = setInterval(mouseSpray, 50);
}
private function mouseUp(e:MouseEvent):void {
clearInterval(ID);
}
private function mouseClick(e:MouseEvent):void {
var _ball:Particle = new Particle(_bar.x + _bar.width/2, _bar.y);
_ball.vx = Math.random()*(2*Math.round(Math.random())-1)*SPEED;
_ball.vy = -Math.random()*2*SPEED -1;
_ball.color = Math.floor(Math.random()*0xFFFFFF);
_balls.push(_ball);
}
private function mouseSpray():void {
for(var i:int = _bar.x; i <= _bar.x + _bar.width; i++) {
var _ball:Particle = new Particle(i, _bar.y);
_ball.vx = Math.random()*(2*Math.round(Math.random())-1)*SPEED;
_ball.vy = -Math.random()*2*SPEED -1;
_ball.color = Math.floor(Math.random()*0xFFFFFF);
_balls.push(_ball);
}
}
private function update(e:Event):void
{
_canvas.lock();
_canvas.colorTransform(_canvas.rect, new ColorTransform (0.9, 0.5, 0.9));
var removeBalls:Vector.<Particle> = new Vector.<Particle>();
for each(var ball:Particle in _balls)
{
var bvx:Number = ball.vx;
var bvy:Number = ball.vy;
var bspeed:Number = Math.sqrt(bvx * bvx + bvy * bvy);
var bradius:Number = Math.atan2(bvy, bvx);
for (var i:int = 0; i < bspeed;i++)
{
ball.vy += 0.01;
ball.x += ball.vx/bspeed;
ball.y += ball.vy/bspeed;
if ((ball.x < 0 && ball.vx < 0) || (ball.x > WIDTH && ball.vx > 0))
{
removeBalls.push(ball);
}
if (ball.y < 0 && ball.vy < 0)
{
removeBalls.push(ball);
}
if (ball.y > HEIGHT)
{
removeBalls.push(ball);
}
_canvas.setPixel(ball.x, ball.y, ball.color);
}
}
removeBalls.forEach(function(b:Particle, ...args):void {
var index:int = _balls.indexOf(b);
if (index != -1)
{
_balls.splice(index, 1);
}
});
_bar.x = stage.mouseX - _bar.width/2;
_canvas.unlock();
}
}
}
class Particle
{
public var x:Number;
public var y:Number;
public var vx:Number = 0;
public var vy:Number = 0;
public var color:uint;
public function Particle(x:Number=0,y:Number=0 )
{
this.x = x;
this.y = y;
}
}