/**
* Copyright sargerasssape ( http://wonderfl.net/user/sargerasssape )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/4i8T
*/
package
{
import caurina.transitions.Tweener;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.ui.Keyboard;
public class Main extends Sprite
{
public var canon:MovieClip = new MovieClip();
// public var _stage:Stage = org.casalib.util.StageReference.getStage();
public var arrayBala:Array = [];
public var conta:Number = 0;
public var dispa:Boolean = false;
public var barrita:Array = [];
public var blokes:Array = [];
public var lvl:Number = 2;
public var clip:Boolean = false;
public function Main()
{
super();
// this.init();
if(this.stage != null)
this.addStage(null);
else
this.addEventListener(Event.ADDED_TO_STAGE,this.addStage);
}
private function addStage(evt:Event):void
{
this.removeEventListener(Event.ADDED_TO_STAGE,this.addStage);
this.init();
}
public function init():void
{
this.canon.graphics.beginFill(0x003300);
this.canon.graphics.drawEllipse(0,0,50,20);
this.addChild(this.canon);
this.canon.x = 50;
this.canon.y = 400;
this.agregarEscenario(this.lvl);
this.stage.addEventListener(MouseEvent.MOUSE_DOWN,this.mouseEncima);
this.addEventListener(Event.ENTER_FRAME,this.onEnterFrame);
}
public function mouseEncima(evt:MouseEvent):void
{
this.clip = true;
this.stage.addEventListener(MouseEvent.MOUSE_UP,this.mouseUp);
this.stage.removeEventListener(MouseEvent.MOUSE_DOWN,this.mouseEncima);
}
public function mouseUp(evt:MouseEvent):void
{
this.clip = false;
this.stage.removeEventListener(MouseEvent.MOUSE_UP,this.mouseUp);
this.stage.addEventListener(MouseEvent.MOUSE_DOWN,this.mouseEncima);
}
public function agregarEscenario(nivel:Number):void
{
for (var i:int=0;i<nivel;i++)
{
var blok:Obstaculos = new Obstaculos();
blok.x = 300+Math.round(Math.random()*100);
blok.y = 20+ Math.round(Math.random()*50);
this.addChild(blok);
this.blokes.push(blok);
}
}
private function onEnterFrame(evt:Event):void
{
var dd:Point = new Point(this.mouseX-this.canon.x,this.mouseY-this.canon.y);
var angulo:Number = Math.atan(dd.y/dd.x)*180/Math.PI;
this.canon.rotation = angulo;
if (this.clip)
{
this.conta ++;
this.dispa = true;
var cargando:Sprite = new Sprite();
cargando.graphics.beginFill(0x000000);
cargando.graphics.drawRect(0,0,5,20);
this.addChild(cargando);
cargando.x = 10+5*this.conta;
cargando.y = 450;
this.barrita.push(cargando);
}
if (!this.clip && this.dispa)
{
var vex:Number = Math.cos(-this.canon.rotation*Math.PI/180);
var vey:Number = Math.sin(-this.canon.rotation*Math.PI/180);
var balita:Balita = new Balita();
balita.dandoVelocidad(this.conta*0.5*vex,-this.conta*0.5*vey,0.1);
balita.x = this.canon.x;
balita.y = this.canon.y;
this.addChild(balita);
this.arrayBala.push(balita);
this.dispa = false;
this.conta = 0;
caurina.transitions.Tweener.addTween(this.barrita,{alpha:0,time:0.2,onComplete:this.termino});
}
var ii:int =0;
for each (var bal:Balita in this.arrayBala)
{
bal.movimiento();
if(bal.compara(this.blokes,this))
{
this.removeChild(bal);
this.arrayBala.splice(ii,1);
}
ii++;
}
if (this.blokes.length ==0)
{
this.lvl++;
this.agregarEscenario(this.lvl);
}
}
private function termino():void
{
this.barrita = [];
}
}
}
import flash.display.MovieClip;
import flash.display.Sprite;
class Balita extends Sprite
{
public var vx:Number;
public var vy:Number;
public var g:Number = 0.5;
function Balita()
{
super();
this.graphics.beginFill(0x123456);
this.graphics.drawCircle(0,0,5);
}
public function dandoVelocidad(v1:Number,v2:Number,g:Number):void
{
this.vx = v1;
this.vy = v2;
this.g = g;
}
public function movimiento():void
{
this.vy += this.g;
this.x +=this.vx;
this.y +=this.vy;
}
public function compara(array:Array,mc:Sprite):Boolean
{
var i:int = 0;
var choco:Boolean =false;
for each (var jj:Sprite in array)
{
var j:int = 0;
if (this.hitTestObject(jj))
{
mc.removeChild(jj);
array.splice(i,1);
choco = true;
}
i++;
}
return choco;
}
}
class Obstaculos extends Sprite
{
function Obstaculos()
{
super();
this.graphics.beginFill(0x000033);
this.graphics.drawRect(0,0,10,50);
}
}