Circular Arkanoid
I was inspired by the bitmapData arkanoid, but I wanted to make it circular, and with less code, so I did.
/**
* Copyright Thumas ( http://wonderfl.net/user/Thumas )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/m6Yk
*/
package {
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.display.BitmapData;
import flash.display.Bitmap
import flash.display.Sprite;
import flash.display.GradientType
import flash.events.Event;
import flash.events.MouseEvent;
[SWF(width=465, height=465, frameRate=30, backgroundColor=0)]
public class FlashTest extends Sprite {
public var staticBmd:BitmapData;
public var parBmd:BitmapData;
public var backBmd:BitmapData;//Cannot get backgroundcolor working :/
public var backBit:Bitmap;
public var w:int = 465;
public var h:int = 465;
public var paddle:Sprite;
public var parList:Vector.<Object>;
public function FlashTest() {
staticBmd = new BitmapData(w,h,true,0);
parBmd = new BitmapData(w,h,true,0);
backBmd = new BitmapData(w,h,false,0);
parList = new Vector.<Object>();
paddle = new Sprite();
paddle.graphics.beginFill(0x00FF00,1);
paddle.graphics.drawRect(-50,-10,100,20);
paddle.graphics.endFill();
var circle:Sprite = new Sprite();
var mat:Matrix=new Matrix();
var colors:Array=[0xFF0000,0xFFFF00,0x00FF00,0x00FFFF,0x0000FF,0xFF00FF,0xFF0000];
var alphas:Array=[1,1,1,1,1,1,1];
var ratios:Array=[0,5,10,15,20,25,30];
circle.graphics.beginGradientFill(GradientType.RADIAL,colors,alphas,ratios,mat);
circle.graphics.drawCircle(0,0,100);
circle.graphics.endFill();
backBit = new Bitmap(backBmd);
addChild(backBit);
addChild(new Bitmap(parBmd));
addChild(new Bitmap(staticBmd));
addChild(paddle);
mat.translate(w*0.5,h*0.5);
staticBmd.draw(circle,mat);
addEventListener(Event.ENTER_FRAME,update);
backBit.addEventListener(MouseEvent.CLICK,click);
}
public function click(e:MouseEvent):void{
if(stage.displayState == "fullScreen"){
stage.displayState = "normal";
}else{
stage.displayState = "fullScreen";
}
}
public function update(e:Event):void{
parBmd.colorTransform(parBmd.rect,new ColorTransform(1,1,1,0.9,0,0,0,0));
var dx:Number = mouseX-(w*0.5);
var dy:Number = mouseY-(h*0.5);
var ang:Number = Math.atan2(dy,dx);
paddle.x = Math.cos(ang)*(w*0.5)+w*0.5;
paddle.y = Math.sin(ang)*(h*0.5)+h*0.5;
paddle.rotation = ang*180/Math.PI+90
if(parList.length ==0){
createParticle(paddle.x,paddle.y,Math.cos(ang)*2,Math.sin(ang)*2,0xFFFFFFFF);
}
for(var i:int = 0;i<parList.length;i++){
var p:Object = parList[i];
p.x += p.vx;
p.y += p.vy;
parBmd.setPixel32(p.x,p.y,p.c);
if(staticBmd.getPixel32(p.x,p.y)!=0){
createParticle(p.x,p.y,-p.vx,-p.vy,staticBmd.getPixel32(p.x,p.y));
staticBmd.setPixel32(p.x,p.y,0);
p.dx= p.x - (w*0.5);
p.dy= p.y - (h*0.5);
p.ang = Math.atan2(p.dy,p.dx);
p.x += Math.cos(p.ang)*5
p.y += Math.sin(p.ang)*5
p.vx = Math.cos(p.ang+Math.random()*0.15-0.075)*2
p.vy = Math.sin(p.ang+Math.random()*0.15-0.075)*2
}
if(paddle.hitTestPoint(p.x,p.y,true)){
p.dx= p.x - (w*0.5);
p.dy= p.y - (h*0.5);
p.ang = Math.atan2(p.dy,p.dx);
var dif:Number = (p.ang-ang)*4;
p.vx = -Math.cos(ang+dif)*2
p.vy = -Math.sin(ang+dif)*2
}
if(p.x>w||p.x<0||p.y>h||p.y<0){
parList.splice(i,1)
}
}
}
public function createParticle(x:Number,y:Number,vx:Number,vy:Number,color:uint):void{
var p:Object = new Object();
p.x = x;
p.y = y;
p.vx = vx;
p.vy = vy;
p.c = color
parList.push(p);
}
}
}