FORKER
Login
|
Register
LazerJuice.as
[
8 revisions
|
fork this
]
Login or register first to fork code
package { import flash.display.*; import flash.text.*; import flash.events.*; import flash.filters.*; import flash.geom.*; import flash.utils.*; public class LazerJuice extends Sprite { private const W:int = 465, H:int = 465; private var cnt:int; private var laserJuice:Number = 100; private var spacing:int = 2; private var updateStep:int = 0; private var ballCount:int = 0; private var ballsPerLevel:int = 1000; private var level:int = 1; private var beamOn:Boolean = false; private var running:Boolean = false; private var mouseDown:Boolean; private var bgCircle:Shape = new Shape(); private var wall:Shape = new Shape(); private var beam:Shape = new Shape(); private var balls:Array = new Array(); private var score:TextField; private var text:TextField; private var juice:TextField; private var traceM:TextField; private var canvas:BitmapData; private var canvasSprite:Bitmap; private var grid:Sprite; private var debugTxt:TextField; [SWF(width = "465", height = "465", backgroundColor = "#000000", frameRate = "30")] public function LazerJuice () { canvas = new BitmapData( stage.stageWidth, stage.stageHeight, false, 0x444444 ); addChild( canvasSprite = new Bitmap( canvas ) ); /*grid = new Sprite(); var g:Graphics = grid.graphics; g.lineStyle(1, 0xffffff, 0.2); const GRID_SIZE:Number = 25; for (var iy:Number = GRID_SIZE * 0.4; iy < stage.stageHeight; iy += GRID_SIZE) { g.moveTo(0, iy); g.lineTo(stage.stageWidth, iy); } for (var ix:Number = GRID_SIZE * 0.2; ix < stage.stageWidth; ix += GRID_SIZE) { g.moveTo(ix, 0); g.lineTo(ix, stage.stageHeight); } addChild(grid);*/ wall.graphics.beginFill(0xffffff); wall.graphics.drawRect(-4, -4, 9, 9); addChild(wall); addChild(text = makeTextField(10, 420, 10)); addChild(juice = makeTextField(10, 450, 10)); addChild(debugTxt = makeTextField(0,0,10,0xffffff)); debugTxt.width = stage.stageWidth; debugTxt.height = stage.stageHeight/2; text.text = 'click to start'; stage.addEventListener(MouseEvent.CLICK, init); stage.addEventListener(MouseEvent.MOUSE_MOVE, laser); stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseState); stage.addEventListener(MouseEvent.MOUSE_UP, mouseState); this.addEventListener(Event.ENTER_FRAME, update); } private function debug(s:*):void { debugTxt.text=debugTxt.text+"n"+s; debugTxt.scrollV = debugTxt.maxScrollV; } public function update(event:Event): void { updateStep ++; var a:Number = Math.atan2(stage.mouseX - W / 2, stage.mouseY - H / 2); // position player aka wall orientWall(a); wall.visible=running ; // Shoot laser if (running) laser(); juice.text = 'lzr: ' + laserJuice.toString(); balls = ballCalc(a); if (running && updateStep >= spacing && ballCount < ballsPerLevel) { updateStep = 0; createBall(a); ballCount ++; } if (ballCount >= ballsPerLevel){levelInit();} // Draw what your want to blur onto the canvas. 'this' for everything, or 'beam' to just blur beam, etc. canvas.draw( beam ); // redraw canvas and make it darker canvas.draw( canvas, null, new ColorTransform(.8, .8, .8, 1, 0, 0, 0, 0),null,null,false); //this.rotationY++; /* if (!grid.transform.matrix3D){ grid.transform.matrix3D = new Matrix3D(); //pivotPoint = new Vector3D(grid.width/2,grid.height/2,0) pivotPoint = new Vector3D(0,0,50) debug([grid.width,grid.height]); } grid.transform.matrix3D.appendRotation(1, Vector3D.Y_AXIS, pivotPoint); */ } private var pivotPoint:Vector3D; public function ballCalc(angle:Number):Array{ var nballs:Array = new Array(); for (var i:int = 0; i < balls.length; i++) { var ball:Shape = balls[i][0]; var ballType:Number = balls[i][3]; //if (ballType==1) canvas.draw( ball ); //quadrentizes the ball var bx:Number = ball.x - W / 2; var by:Number = ball.y - H / 2; var removeBall:Boolean = false; //var da:Number = Math.abs(Math.atan2(bx, by) - angle); // if ((da < 0.02 || da > Math.PI * 2 - 0.02) && Math.abs(Math.sqrt(bx*bx + by*by) - 200) < 4) { if ( running && wall.hitTestObject(ball) ) { removeBall = true; // Check if ball is bad if (ballType == 1){ text.text = ' GAME OVER : CLICK TO START'; running = false; } else { // if ball is good, increase JOOSE! laserJuice = 100; } } //update position ball.x += balls[i][1] * Math.sin(balls[i][2]); ball.y += balls[i][1] * Math.cos(balls[i][2]); //if off the edge remove the ball if (removeBall || ball.x > 0 && ball.x < W && ball.y > 0 && ball.y < H) { nballs.push(balls[i]); } else { removeChild(ball); } } return nballs; } private function makeTextField(x:int, y:int, size:int,color = 0x888888):TextField { var t:TextField = new TextField(); t.x = x; t.y = y; t.width = W; t.height =30; var fm:TextFormat = new TextFormat; fm.color = color; fm.font = '_typewriter'; fm.size = size; t.defaultTextFormat = fm; return t; } public function init(event:MouseEvent): void { if (running){ return; } running = true; cnt = 0; //score.text = 0; laserJuice = 100; juice.text = laserJuice.toString(); text.text = ''; //traceM.text = ''; // makeRings(); } public function levelInit(): void { ballsPerLevel = ballsPerLevel + 10; if (spacing >0){spacing --;} updateStep = 0; ballCount = 0; } public function mouseState(event:MouseEvent): void { if (event.type==MouseEvent.MOUSE_DOWN){ mouseDown = true; } else { mouseDown = false; } } public function createBall(a:Number): void { var alpha:Number = .90; var type:int = 1; var color:Number=0x000000; var glowColor:Number = 0xff0000; var size:Number = 5; // Create a green ball which HELPS you! if ( ballCount == (Math.floor(ballsPerLevel/2))){ color = 0xffffff; glowColor= 0xffffff; alpha = 1; // size = 10; type = 2; } var ball:Shape = new Shape(); ball.graphics.beginFill(color,alpha); ball.graphics.drawCircle(0, 0, size); var glow:GlowFilter = new GlowFilter(); glow.color = glowColor; glow.alpha = 1; glow.blurX = 10; glow.blurY = 10; glow.quality = BitmapFilterQuality.HIGH; var filtersArray:Array = new Array(glow); ball.filters = filtersArray; addChild(ball); ball.x = W / 2; ball.y = H / 2; var t:Number = Math.PI * cnt / 300; var r:Number = Math.PI * 2 * Math.pow(Math.sin(t), 2); balls.push(new Array(ball, Math.random() * 3 + 2, a + Math.random() * r - r / 2,type)); } public function makeRings(): void { bgCircle.graphics.clear(); var h:int; for (h = 0; h <= 10; h++) { bgCircle.graphics.beginFill(0xff0000,.025); bgCircle.graphics.drawCircle(W / 2, W/2, 30 * h); var glow:GlowFilter = new GlowFilter(); glow.color = 0xff0000; glow.alpha = 1; glow.blurX = 10; glow.blurY = 10; glow.quality = BitmapFilterQuality.MEDIUM; var filtersArray:Array = new Array(glow); bgCircle.filters = filtersArray; addChild(bgCircle); } } private var laserglow:GlowFilter = new GlowFilter(0xffffff,1,4,4); private function laser(...rest): void { beam.graphics.clear(); if (mouseDown && laserJuice>0){ beam.graphics.lineStyle(1,0xffffff); beam.graphics.moveTo(wall.x,wall.y); beam.graphics.lineTo(W /2, H /2); //laserglow.blurX = laserglow.blurY = Math.random()*10; laserglow.alpha = Math.random()*2; beam.filters = [laserglow]; addChild(beam); laserJuice -= 5; } } public function orientWall(angle:Number):void{ wall.rotation = - angle / 3.141592 * 180; wall.x = W / 2 + Math.sin(angle) * 200; wall.y = H / 2 + Math.cos(angle) * 200; } } }