glasses.gotoAndStop(1);
	var hidden:Boolean = true;
	var wait:int = 0;
	
	var haveglasses:int = 0;

function dglasses(e:Event):void
{
	var randomNum:Number = Math.random()*10;
	//trace(randomNum);
	
	if (randomNum>5.2 && randomNum<5.23 && !haveglasses)
	{
	//	trace("aclass");
		hidden = false;
		
	}
	
	if (!hidden && wait <=200 && !dead)
	{
			glasses.gotoAndStop(2);
			wait++;
		if(box.hitTestObject(glasses))
			{
			//	trace("fucking aclass");
				wait = 0;
				hidden = true;
				glasses.gotoAndStop(1);
				haveglasses++;
			}

	}
	if (!hidden && wait >200)
	{
	wait = 0;
	hidden = true;
	glasses.gotoAndStop(1);
		
	}
	
	if (haveglasses >0)
	{
		haveglasses++;
	}
	if (haveglasses >300)
	{
		haveglasses=0;
	}

	
}

stage.addEventListener(Event.ENTER_FRAME,dglasses);

stop();
box.gotoAndStop(5);
//box.addEventListener(Event.ENTER_FRAME,move);

//function move(evt:Event):void {
//}
var txtFld:TextField = new TextField();
addChild (txtFld);

//playerVars
//var playermove:String = "still";
var playerSpeed:Number = 10;
var playerLeft:Boolean = false;
var playerRight:Boolean = false;
var playerUp:Boolean = false;
var playerDown:Boolean = false;

//baddie attrs
var baddieangle:Number = 0;
var baddieSpeed:Number = 6;

var diffx:Number = 0;
var diffy:Number = 0;
var zbaddieangle:Number = 0;

var numbaddies:int =0;
var allbaddies:int =100;

var spawnAmount:int = 100;
var visInc:int =0;

var baddieList:Array = new Array();

//bulletVars
var bulletAmount:int = 10;
var bulletList:Array = new Array();
var numbullets:int =0;
var bulletSpeed:Number = 10;
var zbulletangle:Number = 0;
var activateGun:Boolean = false;

//var numBaddies:int = 0;
//function initBaddies():void

stage.addEventListener(MouseEvent.CLICK,  shoot);

function moveBullets(e:Event):void
{
	if(activateGun){
		for (var i:String in bulletList) 
		{
			bulletList[i][6]++;
			//trace (bulletList[i][6]);//increas age
					//trace(bulletList[i][0]);

		var bulletSelect:DisplayObject = getChildByName(bulletList[i][0]);
		//	trace(bulletList[i][0]);
		bulletSelect.x += Math.cos(bulletList[i][1]) * bulletSpeed;
		bulletSelect.y += Math.sin(bulletList[i][1]) * bulletSpeed;
		}

		if(level.hitTestPoint(bulletSelect.x+6,bulletSelect.y, true))
			{
				//trace (String(" hit "));
				bulletSelect.x -=bulletSpeed*1.5;
				}
				if(level.hitTestPoint(bulletSelect.x-6,bulletSelect.y, true))
				{
				//trace (String(" hit "));
				bulletSelect.x +=bulletSpeed*1.5;
				}
				if(level.hitTestPoint(bulletSelect.x,bulletSelect.y-6, true))
				{
				//trace (String(" hit "));
				bulletSelect.y +=bulletSpeed*1.5;
				}
				if(level.hitTestPoint(bulletSelect.x,bulletSelect.y+6, true))
				{
				//trace (String(" hit "));
				bulletSelect.y -=bulletSpeed*1.5;
			}
		}
	
}

	var r:Number = 0;
	var c:Number = 0;

function shoot(e:MouseEvent):void
{activateGun = true;
   // trace("stage X " + e.stageX);
  //  trace("stage Y " + e.stageY);

	    if (numbullets <= bulletAmount) {

				var myAtan2:Number;
 
				myAtan2 = Math.atan2(e.stageY - box.y, e.stageX - box.x);
			//	trace(myAtan2 * 180 / Math.PI) // gives you the angle in degrees;

				bulletList.push(["bullet"+String(r), myAtan2,box.x,box.y,e.stageX,e.stageY,0]);
				var shot:MovieClip = new Bullet();
				shot.name = bulletList[r][0];
				addChild(shot);
				var shotSelect:DisplayObject = getChildByName(bulletList[r][0]);
				shotSelect.x = bulletList[r][2] ;
				shotSelect.y = bulletList[r][3];
			
				numbullets++;
			r++;
         }
		 else if (r > bulletAmount)
		 {

			 		myAtan2 = Math.atan2(e.stageY - box.y, e.stageX - box.x);
			//	trace(myAtan2 * 180 / Math.PI) // gives you the angle in degrees;
				bulletList[c][1]=myAtan2;
				bulletList[c][2]=box.x;
				bulletList[c][3]=box.y;
				bulletList[c][4]=e.stageX;
				bulletList[c][5]=e.stageY;
				bulletList[c][6]=0;
				shotSelect = getChildByName(bulletList[c][0]);
				shotSelect.x = bulletList[c][2] ;
				shotSelect.y = bulletList[c][3];

						 c++;

			 if (c > bulletAmount)
			 {
				 c=0;
			 }
			 
			  //trace(bulletAmount);
			// trace(c);
			// trace(r);

		 }
}

/*	for(var i:int=0 ; i<spawnAmount; i++)
	{
		var zombie:MovieClip = new Baddie();
		zombie.x = 440 +(Math.random()*40) ;
		zombie.y = -10 + Math.random()*5;
		zombie.name = "baddie"+String(i);
		zombie.alpha  = 100;
		addChildAt(zombie, 0);
		numbaddies++;
	}*/
	
//	var zombieSelect:DisplayObject = getChildByName("baddie3");
//	zombieSelect.y = 200;
//	zombieSelect = getChildByName("baddie2");
//	zombieSelect.y = 400;
	
	//numBaddies++;

function mouseMoveListener(e:MouseEvent):void {
     
    var angle:Number;
 
    var turnTurretToX:Number = e.stageX;
    var turnTurretToY:Number = e.stageY;

    var turretAtan2:Number = Math.atan2(turnTurretToY - box.y, turnTurretToX - box.x);
 
    if (Math.round(turretAtan2 * 180 / Math.PI) < 0) {
        angle = Math.round(turretAtan2 * 180 / Math.PI) + 360;
    } else {
        angle = Math.round(turretAtan2 * 180 / Math.PI);
    }

     
    if (angle > 240 && angle < 300) {

        box.gotoAndStop(1);
    }
    if (angle > 300 && angle < 340) {
        box.gotoAndStop(2);
    }
    if ((angle >= 0 && angle < 20) || (angle > 340 && angle <= 360)) {
        box.gotoAndStop(3);
    }
    if (angle > 20 && angle < 60) {
        box.gotoAndStop(4);
    }
    if (angle > 60 && angle < 120) {
		if(haveglasses)
		{
			box.gotoAndStop("downGlasses");
		}
		else{
        box.gotoAndStop(5);
		}
    }
    if (angle > 120 && angle < 160) {
        		if(haveglasses)
		{
			box.gotoAndStop("downGlasses");
		}
		else{
        box.gotoAndStop(6);
		}
    }
    if (angle > 160 && angle < 200) {
        box.gotoAndStop(7);;
    }
    if (angle > 200 && angle < 240) {
        box.gotoAndStop(8);
    } 
     
}

	
	
var fade:int = 0;

function spriteFlicker(e:Event):void
{

	if (baddieArraylength<5)
	{
		fade= baddieArraylength;
	}
	else
	{
		fade=5;
	}

	for(var i:int=1 ; i<=fade; i++)
	{
		{var sel:int = visInc-i;
		if(sel <0)
		{
			sel = sel + baddieArraylength;
		}
		if (sel<baddieArraylength){
		var zombieSelect:DisplayObject = getChildByName(baddieList[sel]);
	
		if(!haveglasses)
		{zombieSelect.alpha = Math.abs( 1-i/fade);
		}
		else
			if(haveglasses)
			{
				zombieSelect.alpha = 1;
			}
		
		}
	}

}
	if (baddieArraylength==1)
	{
		zombieSelect = getChildByName(baddieList[0]);
		zombieSelect.alpha = 1;	
	}

	
		if (visInc <baddieArraylength){
		visInc++;
		}
		else{
			visInc= 0;
		}

	
}
//box.alpha = 1;	

function moveBaddies(e:Event):void
{
	
for (var i:String in baddieList) 
{ 
    //trace(baddieList[i]); 

	
		
	var zdiffx:Number = 0;
	var zdiffy:Number = 0;	
	//select thing from dispay list
	var zombieSelect:DisplayObject = getChildByName(baddieList[i]);
	if (zombieSelect!= null){
		zdiffx=box.x-zombieSelect.x;
		zdiffy=box.y-zombieSelect.y;

		zbaddieangle = Math.atan2(zdiffy, zdiffx);

			if (zdiffx * zdiffx + zdiffy * zdiffy < baddieSpeed * baddieSpeed)
			{
				zombieSelect.x = box.x;
				zombieSelect.y = box.y;
			}
			else{

				zombieSelect.x += Math.cos(zbaddieangle) * baddieSpeed;
				zombieSelect.y += Math.sin(zbaddieangle) * baddieSpeed;
				//zombieSelect.rotation = zbaddieangle * 180 / Math.PI +100;
				
			}

			if(level.hitTestPoint(zombieSelect.x+26,zombieSelect.y, true))
			{if (otherzombieSelect!= null){
			//trace (String(" hit "));
			zombieSelect.x -=baddieSpeed;}
			}
			if(level.hitTestPoint(zombieSelect.x-26,zombieSelect.y, true))
			{if (otherzombieSelect!= null){
			//trace (String(" hit "));
			zombieSelect.x +=baddieSpeed;}
			}
			if(level.hitTestPoint(zombieSelect.x,zombieSelect.y-22, true))
			{if (otherzombieSelect!= null){
			//trace (String(" hit "));
			zombieSelect.y +=baddieSpeed;}
			}
			if(level.hitTestPoint(zombieSelect.x,zombieSelect.y+22, true))
			{if (otherzombieSelect!= null){
			//trace (String(" hit "));
			zombieSelect.y -=baddieSpeed;}
			}

			for (var j:String in baddieList) 
		{ 
	
		var otherzombieSelect:DisplayObject = getChildByName(baddieList[j]);
			if(otherzombieSelect != zombieSelect && zombieSelect != null && otherzombieSelect != null){
				if(zombieSelect.hitTestPoint(otherzombieSelect.x+26,otherzombieSelect.y, true))
				{
				//trace (String(" hit "));
				if (otherzombieSelect!= null){
				otherzombieSelect.x -=baddieSpeed;}
				}
				if(zombieSelect.hitTestPoint(otherzombieSelect.x-26,otherzombieSelect.y, true))
				{if (otherzombieSelect!= null){
				//trace (String(" hit "));
				otherzombieSelect.x +=baddieSpeed;}
				}
				if(zombieSelect.hitTestPoint(otherzombieSelect.x,otherzombieSelect.y-22, true))
				{if (otherzombieSelect!= null){
				//trace (String(" hit "));
				otherzombieSelect.y +=baddieSpeed;}
				}
				if(zombieSelect.hitTestPoint(otherzombieSelect.x,otherzombieSelect.y+22, true))
				{if (otherzombieSelect!= null){
				//trace (String(" hit "));
				otherzombieSelect.y -=baddieSpeed;}
				}
			
			}

		}
		
		//bulletHitCode----_____----_____---___-____-_-_____--
		
		for (var k:String in bulletList) 						
		{ 
			var bulletSelect:DisplayObject = getChildByName(bulletList[k][0]);

			if(zombieSelect.hitTestObject(bulletSelect))
			{
				//trace (String (zombieSelect.name +" HIT BY " + bulletSelect.name));
				
				if (zombieSelect!= null){
					
///---bloodAnim--------------------
				var gore:MovieClip = new MonsterBlood();
				gore.name = ("MonsterBlood" + baddieNamer);
				addChild(gore);
				var goreSelect:DisplayObject = getChildByName("MonsterBlood" + baddieNamer);
				if(zombieSelect!= null && goreSelect!= null){
					
				goreSelect.x = zombieSelect.x;
				goreSelect.y = zombieSelect.y;
				}
///----blodadsfd---

					

					
					removeChild(zombieSelect);
						//trace(baddieList[i]);

				//	trace(baddieList[i]);
					
					baddieList.splice([i],1);
					bulletList[k][1] *= -1;
					break;
					//trace (String "removed"  zombieSelect
				}
			}

		}
}
}
}	

/*function moveBaddie(e:Event):void
{
	diffx=box.x-baddie.x;
	diffy=box.y-baddie.y;

	baddieangle = Math.atan2(diffy, diffx);

        if (diffx * diffx + diffy * diffy < baddieSpeed * baddieSpeed)
        {
            baddie.x = box.x;
            baddie.y = box.y;
        }
        else
        {
		baddie.x += Math.cos(baddieangle) * baddieSpeed;
		baddie.y += Math.sin(baddieangle) * baddieSpeed;
		baddie.rotation = baddieangle * 180 / Math.PI +100;
	}

}*/

var skip:Number = 10;
var baddieNamer:Number = 0;
	var baddieArraylength:Number = 0;

function enterFrame(evt:Event):void {

baddieArraylength =0;

	
	//txtFld.text = String("playerLeft: " + playerLeft +"\nplayerRight:" + playerRight +"\nplayerUp:" + playerUp+"\nplayerDown:" + playerDown 
	//+"\nbaddieangle:" + (baddieangle * 180/Math.PI) + "\ndiffx: " + diffx + "\ndiffy: " + diffy + "\nvisInc: " + visInc );
setChildIndex(box, numChildren-4);

	setChildIndex(restart, numChildren-4);
	setChildIndex(scanlines, numChildren-3);
	setChildIndex(glare, numChildren-2);
		setChildIndex(tvframe, numChildren-1);
	for (var i:String in baddieList) 
{ 
	//trace(baddieList[i]);
	baddieArraylength++;
	
}
//trace(String("baddieArraylength - " + baddieArraylength ));

	
	
		if (baddieArraylength< allbaddies && skip >= 10 )
	{

				baddieList.push("baddie" + baddieNamer);
				var zombie:MovieClip = new Baddie();
				zombie.name = ("baddie" + baddieNamer);

				addChild(zombie);
				var zombieSelect:DisplayObject = getChildByName("baddie" + baddieNamer);
				zombieSelect.x = 355 ;
				zombieSelect.y = 46;
				zombieSelect.alpha  = 0;
				numbaddies++;
				baddieNamer++;
				skip =0;
         
	 }
	 else{
		 skip++;
	 }

	//box wall hitTest 
	 
	 			if(level.hitTestPoint(box.x+18,box.y, true))
				{
				//trace (String(" hit "));
				box.x -=playerSpeed;
				}
				if(level.hitTestPoint(box.x-18,box.y, true))
				{
				//trace (String(" hit "));
				box.x +=playerSpeed;
				}
				if(level.hitTestPoint(box.x,box.y-25, true))
				{
				//trace (String(" hit "));
				box.y +=playerSpeed;
				}
				if(level.hitTestPoint(box.x,box.y+25, true))
				{
				//trace (String(" hit "));
				box.y -=playerSpeed;
				}
		
}

function onKeyPressed(evt:KeyboardEvent):void {
	switch (evt.keyCode) {
		case Keyboard.A:
			playerLeft = true;
			break;
		
		case Keyboard.D:
			playerRight = true;
			break;
		
		case Keyboard.W:
			playerUp = true;
			break;
				
		case Keyboard.S:
			playerDown = true;
			break;

	default:
		//trace ("keycode:",evt.keyCode);
		break;

	}	

};

function onKeyReleased(evt:KeyboardEvent):void {
	switch (evt.keyCode) {
		case Keyboard.A:
			playerLeft = false;
			break;
		
		case Keyboard.D:
			playerRight = false;
			break;
		
		case Keyboard.W:
			playerUp = false;
			break;
				
		case Keyboard.S:
			playerDown = false;
			break;

	default:
		break;

	}	

};

var dead:Boolean = false;

function movePlayer(evt:Event):void {
	if (!dead){
	
	for (var i:String in baddieList) 
	{ 
		var zombieSelect:DisplayObject = getChildByName(baddieList[i]);
		if (zombieSelect!= null)
		{
			if(box.hitTestObject(zombieSelect))
			{
				//trace("dead");
				dead = true;
			}
		}
		
	}
	
if(activateGun){
	for (var k:String in bulletList) 						
		{ 
			var bulletSelect:DisplayObject = getChildByName(bulletList[k][0]);

			if(box.hitTestObject(bulletSelect))
			{
				//trace (String (zombieSelect.name +" HIT BY " + bulletSelect.name));
				
				if (box!= null && bulletList[k][6]>30){
					
				//trace("dead");
				dead = true;

			}
		}
			
	}
	}

	

	
	
	if (playerLeft){
		box.x -= playerSpeed;
	}
	
	if (playerRight){
		box.x += playerSpeed;
	}
	
	if (playerUp){
			box.y -= playerSpeed;
	}
		
	if (playerDown)
	{
		box.y += playerSpeed;
	}
}
else
{
	box.gotoAndStop("dead");
}

	
}
stage.addEventListener(Event.ENTER_FRAME,enterFrame);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyReleased);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
stage.addEventListener(Event.ENTER_FRAME,movePlayer);
//stage.addEventListener(Event.ENTER_FRAME,moveBaddie);
stage.addEventListener(Event.ENTER_FRAME,spriteFlicker);
stage.addEventListener(Event.ENTER_FRAME,moveBullets);
stage.addEventListener(Event.ENTER_FRAME,moveBaddies);

stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener);

StageQuality.LOW;
var restartNum: int = 0;
import flash.utils.Timer;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.globalization.DateTimeFormatter;

var timer:Timer = new Timer(100);
timer.start();
timer.addEventListener(TimerEvent.TIMER, timerTickHandler);
var timerCount:int = 0;
function timerTickHandler(Event:TimerEvent):void
{
	if (!dead){
	
	timerCount += 100;
	toTimeCode(timerCount);
		}
		else
		{
			if (restartNum<=0)
			{
				restart.play();
				restartNum++;
			}
			else
			{
				restartNum++;
				//trace (restartNum);
			}
		}
}

function toTimeCode(milliseconds:int) : void {
	//create a date object using the elapsed milliseconds
	var time:Date = new Date(milliseconds);
	
	//define minutes/seconds/mseconds
	var minutes:String = String(time.minutes);
	var seconds:String = String(time.seconds);
	var miliseconds:String = String(Math.round(time.milliseconds)/100);
	
	//add zero if neccecary, for example: 2:3.5 becomes 02:03.5
	minutes = (minutes.length != 2) ? '0'+minutes : minutes;
	seconds = (seconds.length != 2) ? '0'+seconds : seconds;
	
	//display elapsed time on in a textfield on stage
	timer_txt.text = minutes + ":" + seconds+"." + miliseconds;

}

addEventListener(MouseEvent.CLICK,  respawn);

function respawn(e:MouseEvent):void
{
 if (dead && restartNum >10){
	 	var _scope:DisplayObjectContainer = this; // set the desired scope here, or below if you want to keep it to one line
    while(_scope.numChildren > 0) _scope.removeChildAt(_scope.numChildren-1);
	this.gotoAndStop(2);

 }
}