Guest User

Skirmish

a guest
Dec 19th, 2014
1,099
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package
  2. {
  3.     import flash.display.MovieClip;
  4.     import flash.events.Event;
  5.     import flash.events.KeyboardEvent;
  6.     import flash.ui.Keyboard;
  7.     import flash.events.MouseEvent;
  8.     import flash.text.TextField;
  9.  
  10.     public class Skirmish extends MovieClip
  11.     {
  12.         var pressed1:Boolean = false;
  13.  
  14.         var timer:int = 0;
  15.         var enemyTimer:int = 0;
  16.         var yourSpendingPoints:int = 100;
  17.         var enemySpendingPoints:int = 100;
  18.        
  19.         var textPoints:TextField=new TextField();
  20.        
  21.  
  22.  
  23.  
  24.         var Enemies:Array = [];
  25.         var Allies:Array = [];
  26.  
  27.         public function Skirmish()
  28.         {
  29.             stage.addEventListener(Event.ENTER_FRAME,MainLoop);
  30.             stage.addEventListener(KeyboardEvent.KEY_DOWN,KeysDown);
  31.  
  32.         }
  33.         public function MainLoop(e:Event)
  34.         {
  35.             textPoints.x=10;
  36.             textPoints.y=10;
  37.             addChild(textPoints);
  38.             textPoints.text="VicePoints:"+yourSpendingPoints;
  39.            
  40.            
  41.            
  42.             timer++;
  43.             enemyTimer++;
  44.  
  45.             handleUserSpawning();
  46.             handleEnemySpawning();
  47.  
  48.             doYourSpawnedAi();
  49.             doEnemySpawnedAi();
  50.  
  51.             handleSkirmishes();
  52.  
  53.             addSpendingPoints();
  54.  
  55.        
  56.         }
  57.  
  58.         function KeysDown(e:KeyboardEvent)
  59.         {
  60.             switch (e.keyCode)
  61.             {
  62.  
  63.                 case 49 :
  64.  
  65.                     pressed1 = true;
  66.                     trace("pressed 1");
  67.                     //spawnEnemy(1);
  68.                     break;
  69.  
  70.                     // eventually ill expand this to do cases 49-56 keys 1-8
  71.                     // each key will summon a new entity
  72.             }
  73.  
  74.         }
  75.         public function handleUserSpawning()
  76.         {
  77.             if (pressed1==true&&(yourSpendingPoints-25)>=0&&timer%60==0)
  78.             {//&&timer%60==0
  79.                 var e = new Enemy(0,2,50,1);
  80.                 addChild(e);
  81.                 Enemies.push(e);
  82.                 yourSpendingPoints -=  25;
  83.                 pressed1 = false;
  84.  
  85.             }
  86.         }
  87.         public function handleEnemySpawning()
  88.         {
  89.             if ((enemySpendingPoints-25)>=0&&timer%60==0)
  90.             {
  91.                 var a = new Enemy(330,-2,50,-1);
  92.  
  93.                 addChild(a);
  94.                 Allies.push(a);
  95.                 enemySpendingPoints -=  25;
  96.                
  97.  
  98.             }
  99.  
  100.         }
  101.  
  102.  
  103.         public function doYourSpawnedAi()
  104.         {
  105.             for (var i:int=Enemies.length-1; i>=0; i--)
  106.             {
  107.                 Enemies[i].doAi();
  108.                
  109.                 if (Enemies[i].x >= 330)
  110.                 {
  111.                    
  112.                     Enemies[i].canMove = false;
  113.                     Enemies[i].shouldAttack = true;
  114.                     trace("time to stop moving and start attacking");
  115.                 }
  116.             }
  117.  
  118.         }
  119.  
  120.         public function addSpendingPoints()
  121.         {
  122.             if (timer%24==0)
  123.             {
  124.                 yourSpendingPoints +=  5;
  125.             }
  126.             if (timer%36==0)
  127.             {
  128.                 enemySpendingPoints +=  5;
  129.             }
  130.         }
  131.  
  132.        
  133.         public function doEnemySpawnedAi()
  134.         {
  135.             for (var i:int=Allies.length-1; i>=0; i--)
  136.             {
  137.                
  138.                 Allies[i].doAi();
  139.                
  140.                     if (Allies[i].x <= 30+Allies[i].width/2)
  141.                     {
  142.                     Allies[i].x+=2;
  143.                     Allies[i].shouldAttack = true;
  144.                         if(Allies[i].shouldAttack ==true&&timer%200==0)
  145.                            {
  146.                                 removeChild(Allies[i]);
  147.                                 Allies.splice(i,1);
  148.                                
  149.                            }
  150.                     trace("time to stop moving and start attacking");
  151.                     }
  152.                 for(var j=i-1;j>=0;j--)
  153.                 {
  154.                     if(Allies[j].hitTestObject(Allies[i]))
  155.                        {
  156.                           // Allies[i].canMove = false;
  157.                            Allies[i].x+=2;
  158.                            
  159.                        }
  160.                 }
  161.                
  162.                
  163.             }
  164.         }
  165.  
  166.         public function handleSkirmishes()
  167.         {
  168.             for (var i:int =Enemies.length-1; i>=0; i--)
  169.             {
  170.  
  171.                 for (var j:int =Allies.length-1; j>=0; j--)
  172.                 {
  173.  
  174.                     if (Enemies[i].hitTestObject(Allies[j]))
  175.                     {
  176.                         Enemies[i].canMove = false;
  177.                         Enemies[i].shouldAttack = true;
  178.                         removeChild(Enemies[i]);
  179.                         Enemies.splice(i,1);
  180.                         removeChild(Allies[j]);
  181.                         Allies.splice(j,1);
  182.                         // was getting a bug here error 1010
  183.                         // the for loop was trying to check the removed bullet and enemy for collision
  184.                         // break statement inserted to fix the error becuase if they are removed no need to check
  185.  
  186.                         break;
  187.  
  188.  
  189.                     }
  190.                 }
  191.             }
  192.         }
  193.         ///*************************************************************************
  194.     }
  195.  
  196. }
Advertisement
Add Comment
Please, Sign In to add comment