Guest User

Untitled

a guest
Jun 15th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //-------------------------------------------------------------------------------------------------
  2. //Program: PSP_Assignment2_Jon(s10105455k)_Shafi(s10087528k)
  3. //Created by: Jon Michael Jubelag & Adam Adil Shafi
  4. //Date Created: Week 13
  5. //Date Modified: 2nd Feb 2012
  6. //Description: The Assignment 2
  7. //------------------------------------------------------------------------------------------------
  8.  
  9.  
  10. package
  11. {
  12.     import flash.display.*;
  13.     import flash.utils.Dictionary;
  14.     import flash.utils.Timer;
  15.     import flash.events.*;
  16.     import flash.media.Sound;
  17.     import flash.events.MouseEvent;
  18.     import flash.media.SoundChannel;
  19.     import flash.ui.Mouse;
  20.    
  21.     public class GameControl extends MovieClip
  22.     {
  23.         //Properties
  24.         private var platforms:Dictionary;
  25.         private var walls:Dictionary;
  26.         private var enemies:Dictionary;
  27.         private var hpDrops:Dictionary;
  28.         private var endPoints:Dictionary;
  29.         private var item:Object;       
  30.        
  31.         private var player:MovieClip;
  32.         private var playerHealth:Number;
  33.        
  34.         private var gameTimer:Timer;
  35.         private var playTimer:Timer;
  36.        
  37.         private var yDiff:Number;
  38.         private var points:Number;
  39.         private var bonusPoints:Number;
  40.        
  41.         private var suspense1:Sound;
  42.         private var suspense2:Sound;
  43.  
  44.         private var suspense1Channel:SoundChannel;
  45.         private var suspense2Channel:SoundChannel;
  46.  
  47.         private var musicTimer1:Timer;
  48.         private var musicTimer2:Timer;
  49.  
  50.         private var level:MovieClip;
  51.         private var currentLevel:Number;
  52.         private var lastLevel:Boolean;
  53.        
  54.         private var secretHere:Boolean;
  55.         private var hitSecret:Boolean;
  56.        
  57.         public function GameControl()
  58.         {
  59.             //Constructor
  60.             gameTimer = new Timer(30, 0);
  61.             gameTimer.addEventListener(TimerEvent.TIMER, gameLoop);
  62.            
  63.             playTimer = new Timer(1000, 120);
  64.             playTimer.addEventListener(TimerEvent.TIMER, time);
  65.            
  66.             platforms = new Dictionary();
  67.             walls = new Dictionary();
  68.             enemies = new Dictionary();
  69.             hpDrops = new Dictionary();
  70.             endPoints = new Dictionary();
  71.             mainMenu.playButton.addEventListener(MouseEvent.CLICK, playGame);
  72.             mainMenu.creditsButton.addEventListener(MouseEvent.CLICK, showCredits);
  73.             creditsMenu.backButton.addEventListener(MouseEvent.CLICK, goBack);
  74.             endMenu.menuButton.addEventListener(MouseEvent.CLICK, toMainMenu);
  75.             storyMenu.continueButton.addEventListener(MouseEvent.CLICK, continueTo);
  76.             gameOverMenu.playAgainButton.addEventListener(MouseEvent.CLICK, playAgain);
  77.            
  78.             suspense1 = new music1();
  79.             suspense2 = new music2();
  80.            
  81.             suspense1Channel = new SoundChannel();
  82.             suspense2Channel = new SoundChannel();
  83.            
  84.             musicTimer1 = new Timer (55000, 1);
  85.             musicTimer1.addEventListener(TimerEvent.TIMER_COMPLETE, musicTime1);
  86.             musicTimer2 = new Timer (6000, 1);
  87.             musicTimer2.addEventListener(TimerEvent.TIMER_COMPLETE, musicTime2);
  88.            
  89.             //Create the player
  90.             player = new Player(this);
  91.             currentLevel = 1;
  92.  
  93.             creditsMenu.visible = false;
  94.             storyMenu.visible = false;
  95.             filmGrain.visible = false;
  96.             endMenu.visible = false;
  97.             gameOverMenu.visible = false;
  98.             fullHealth.visible = false;
  99.             secretFound.visible = false;
  100.         }
  101.        
  102.        
  103.         public function initGame() //initializes the game
  104.         {
  105.             if(currentLevel == 1)
  106.             {
  107.                 level = new Level1();
  108.                 level.name = "level";
  109.                 level.x = -2.7;         //Spawns level with regard to displacement from stage's origin.
  110.                 level.y = -330.1;
  111.                 this.addChild(level);
  112.                 this.setChildIndex(level,1);
  113.                 lastLevel = false;
  114.                 player.x = 200;
  115.                 player.y = 100;
  116.                 points = 0;
  117.                 secretHere = true;
  118.                 hitSecret = true;
  119.             }
  120.            
  121.             else if(currentLevel == 2)
  122.             {
  123.                 clearDict();                //Clears all items in all dictionaries before adding them for the next level.
  124.                
  125.                 level = new Level2();
  126.                 level.name = "level";
  127.                 level.x = -2.7;
  128.                 level.y = -330.1;
  129.                 this.addChild(level);
  130.                 this.setChildIndex(level,1);
  131.                 player.x = 200;
  132.                 player.y = 100;
  133.                 lastLevel = true;           //Endgame content changes.
  134.                 secretHere = false;
  135.             }
  136.  
  137.            
  138.             this.yDiff = this.level.y;      //Get the distance between the level and the stage's origin.
  139.             level.visible = true;
  140.             player.visible = true;
  141.            
  142.             for (var i:int = 0; i < this.level.numChildren; i++)
  143.             {
  144.                 item = this.level.getChildAt(i)
  145.                 if (item is Platform)
  146.                 {
  147.                     platforms[item] = item; //Adds platform to dictionary
  148.                 }
  149.                
  150.                 else if (item is Wall)
  151.                 {
  152.                     walls[item] = item;
  153.                 }
  154.                
  155.                 else if (item is Enemy)
  156.                 {
  157.                     item.toMain(this);
  158.                     enemies[item] = item;
  159.                 }
  160.                
  161.                 else if (item is Pickups)
  162.                 {
  163.                     hpDrops[item] = item;
  164.                 }
  165.                
  166.                 else if (item is EndPoint)
  167.                 {
  168.                     endPoints[item] = item;
  169.                 }
  170.             }
  171.            
  172.             this.addChild(player); //add player to main timeline
  173.             this.playerHealth = 5;
  174.             checkHealth();
  175.            
  176.             //Start Timer
  177.             gameTimer.start();
  178.             playTimer.start();
  179.        
  180.             bonusPoints = 120;
  181.            
  182.             suspense1.play();
  183.             musicTimer1.start();
  184.         }
  185.        
  186. //-----------------------------------------------------------------------------------------------------------------------//
  187. //------------------------------------------------MAIN MENU BUTTON CODES-------------------------------------------------//
  188. //-----------------------------------------------------------------------------------------------------------------------//    
  189.  
  190.         public function playGame(e:MouseEvent):void
  191.         {
  192.             mainMenu.visible = false;
  193.             filmGrain.visible = true;
  194.             initGame();
  195.         }
  196.        
  197.         public function showCredits(e:MouseEvent):void
  198.         {
  199.             creditsMenu.visible = true;
  200.         }
  201.        
  202.         public function goBack(e:MouseEvent):void
  203.         {
  204.             creditsMenu.visible = false;
  205.         }
  206.        
  207.         public function continueTo(e:MouseEvent):void
  208.         {
  209.             currentLevel += 1;
  210.             initGame();
  211.             storyMenu.visible = false;
  212.             filmGrain.visible = true;
  213.         }
  214.        
  215.         public function toMainMenu(e:MouseEvent):void
  216.         {
  217.             endMenu.visible = false;
  218.             mainMenu.visible = true;
  219.         }
  220.        
  221.         public function playAgain(e:MouseEvent):void
  222.         {
  223.             initGame();
  224.             gameOverMenu.visible = false;
  225.             filmGrain.visible = true;
  226.         }
  227. //-----------------------------------------------------------------------------------------------------------------------//
  228. //--------------------------------------------------GAME LOOP CODE-------------------------------------------------------//
  229. //-----------------------------------------------------------------------------------------------------------------------//    
  230.        
  231.         //Method: Game Loop
  232.         private function gameLoop(evt:TimerEvent):void  //the loop keeps the player going and updates the textboxes.
  233.         {
  234.             this.player.movePlayer(); //move player
  235.             this.player.shootBullet()//
  236.             showFullHealth();
  237.             secretCode();
  238.            
  239.             bonus.text = bonusPoints.toString();
  240.             pointsText.text = points.toString();
  241.         }
  242.        
  243.         //Method: Bonus Timer
  244.         public function time(evt:TimerEvent):void //this counts down the bonus points. (acts as a challenge for players to get the most points)
  245.         {
  246.             bonusPoints = 120 - playTimer.currentCount;
  247.         }
  248.        
  249. //-----------------------------------------------------------------------------------------------------------------------//
  250. //------------------------------------------------------MUSIC CODE-------------------------------------------------------//
  251. //-----------------------------------------------------------------------------------------------------------------------//    
  252.         //Method: Music Timers
  253.         public function musicTime1(evt:TimerEvent):void
  254.         {
  255.                 suspense2.play();
  256.                 musicTimer2.start();
  257.                 musicTimer1.reset();
  258.         }
  259.        
  260.         public function musicTime2(evt:TimerEvent):void
  261.         {
  262.                 suspense1.play();
  263.                 musicTimer2.reset();
  264.         }
  265.        
  266.        
  267.        
  268. //-----------------------------------------------------------------------------------------------------------------------//
  269. //-------------------------------------------------------GET CODES-------------------------------------------------------//
  270. //-----------------------------------------------------------------------------------------------------------------------//    
  271.        
  272.         //Method: Get Platforms
  273.         public function getPlatforms():Dictionary
  274.         {
  275.             return this.platforms;
  276.         }
  277.        
  278.         //Method: Get Walls
  279.         public function getWalls():Dictionary
  280.         {
  281.             return this.walls;
  282.         }
  283.        
  284.         //Method: Get Enemies
  285.         public function getEnemies():Dictionary
  286.         {
  287.            
  288.             return this.enemies;
  289.         }
  290.        
  291.         // Method: Move vertically
  292.         public function moveCamVertical(iDy:Number)
  293.         {
  294.             this.level.y += iDy;
  295.             this.yDiff += iDy;
  296.         }
  297.        
  298.         //Method: Get yDiff
  299.         public function getYDiff():Number
  300.         {
  301.             return this.yDiff;
  302.         }
  303.        
  304.         //Method: Get Player
  305.         public function getPlayer():MovieClip
  306.         {
  307.             return this.player;
  308.         }
  309.        
  310.         //Method: Get hpDrops
  311.         public function getHpDrops():Dictionary
  312.         {
  313.             return this.hpDrops;
  314.         }
  315.        
  316.         //Method: Get endPoints
  317.         public function getEndPoints():Dictionary
  318.         {
  319.             return this.endPoints;
  320.         }
  321.        
  322.         //Method: Get LastLevel
  323.         public function getLastLevel():Boolean
  324.         {
  325.             return this.lastLevel;
  326.         }
  327.    
  328. //-----------------------------------------------------------------------------------------------------------------------//
  329. //----------------------------------------------PLAYER HEALTH CODES------------------------------------------------------//
  330. //-----------------------------------------------------------------------------------------------------------------------//
  331.    
  332.         //Method: MinusHealth
  333.         public function minusHealth()
  334.         {
  335.             playerHealth -= 1;
  336.         }
  337.        
  338.         //Method: AddHealth
  339.         public function addHealth()
  340.         {
  341.             playerHealth += 1;
  342.             checkHealth();
  343.         }
  344.        
  345.         //Method: CheckHealth
  346.         public function checkHealth() //HealthBars show according to the player's health
  347.         {
  348.             if (playerHealth == 5)
  349.             {
  350.                 hB5.visible = true;
  351.                 hB4.visible = true;
  352.                 hB3.visible = true;
  353.                 hB2.visible = true;
  354.                 hB1.visible = true;
  355.             }
  356.            
  357.             else if (playerHealth == 4)
  358.             {
  359.                 hB5.visible = false;
  360.                 hB4.visible = true;
  361.                 hB3.visible = true;
  362.                 hB2.visible = true;
  363.                 hB1.visible = true;
  364.             }
  365.            
  366.             else if (playerHealth == 3)
  367.             {
  368.                 hB4.visible = false;
  369.                 hB3.visible = true;
  370.                 hB2.visible = true;
  371.                 hB1.visible = true;
  372.             }
  373.            
  374.             else if (playerHealth == 2)
  375.             {
  376.                 hB3.visible = false;
  377.                 hB2.visible = true;
  378.                 hB1.visible = true;
  379.             }
  380.            
  381.             else if (playerHealth == 1)
  382.             {
  383.                 hB2.visible = false;
  384.                 hB1.visible = true;
  385.             }
  386.            
  387.             else if (playerHealth == 0)
  388.             {
  389.                 hB1.visible = false;
  390.                 gameOver();
  391.             }
  392.            
  393.             else if (playerHealth > 5)
  394.             {
  395.                 playerHealth = 5;
  396.             }
  397.         }
  398.        
  399.         //Method: Show fullHealth
  400.         public function showFullHealth()
  401.         {
  402.             if(playerHealth == 5)
  403.             {
  404.                 fullHealth.visible = true;
  405.             }
  406.            
  407.             else
  408.             {
  409.                 fullHealth.visible = false;
  410.             }
  411.         }
  412.        
  413. //-----------------------------------------------------------------------------------------------------------------------//
  414. //----------------------------------------------------POINTS CODES-------------------------------------------------------//
  415. //-----------------------------------------------------------------------------------------------------------------------//    
  416.        
  417.         public function addPoints()
  418.         {
  419.             points += 100;
  420.         }
  421.        
  422.        
  423. //-----------------------------------------------------------------------------------------------------------------------//
  424. //-------------------------------------------------END LEVEL & GAME CODES------------------------------------------------//
  425. //-----------------------------------------------------------------------------------------------------------------------//            
  426.        
  427.        
  428.         public function levelEnd() //ends the level
  429.         {
  430.             points = points + bonusPoints;
  431.            
  432.             //reset all the timers.
  433.             gameTimer.stop();
  434.             gameTimer.reset();
  435.             playTimer.stop();
  436.             playTimer.reset();
  437.            
  438.             //remove all children from the stage
  439.             if(this.contains(player))
  440.             {
  441.                 removeChild(player);
  442.             }
  443.            
  444.             if(this.contains(level))
  445.             {
  446.                 removeChild(level);
  447.             }
  448.            
  449.             if(!lastLevel)
  450.             {
  451.                 //levelUp();
  452.                 player.visible = false;
  453.                 level.visible = false;
  454.                 storyMenu.visible = true;
  455.                 filmGrain.visible = false;
  456.             }
  457.            
  458.             else if (lastLevel)
  459.             {
  460.                 gameEnd();
  461.             }
  462.         }
  463.        
  464.         public function gameEnd()
  465.         {
  466.             currentLevel = 1;
  467.             clearDict();
  468.             filmGrain.visible = false;
  469.             endMenu.visible = true;
  470.             endMenu.finalPoints.text = points;
  471.         }
  472.        
  473.         public function gameOver()
  474.         {
  475.             gameTimer.stop();
  476.             gameTimer.reset();
  477.             playTimer.stop();
  478.             playTimer.reset();
  479.            
  480.             if(this.contains(player))
  481.             {
  482.                 removeChild(player);
  483.             }
  484.            
  485.             if(this.contains(level))
  486.             {
  487.                 removeChild(level);
  488.             }
  489.            
  490.             gameOverMenu.visible = true;
  491.             filmGrain.visible = false;
  492.             gameOverMenu.deathAnimation.gotoAndPlay(1);
  493.         }
  494.        
  495. //-----------------------------------------------------------------------------------------------------------------------//
  496. //-----------------------------------------------CLEAR DICTOINARIES CODES------------------------------------------------//
  497. //-----------------------------------------------------------------------------------------------------------------------//    
  498.        
  499.        
  500.         public function clearDict():void  //Clears all items from all dictionaries
  501.         {
  502.             for (var hp in this.hpDrops)
  503.             {
  504.                 delete hpDrops[hp];
  505.             }
  506.            
  507.             for (var en in this.enemies)
  508.             {
  509.                 delete enemies[en];
  510.             }
  511.            
  512.             for (var wall in this.walls)
  513.             {
  514.                 delete walls[wall];
  515.             }
  516.            
  517.             for (var pf in this.platforms)
  518.             {
  519.                 delete platforms[pf];
  520.             }
  521.  
  522.             for (var ep in this.endPoints)
  523.             {
  524.                 delete endPoints[ep];
  525.             }
  526.         }
  527.        
  528.         //The secret Code
  529.         public function secretCode()
  530.         {
  531.             if (secretHere)
  532.             {
  533.                 if (this.player.hitTestObject(level.secret))
  534.                 {
  535.                     secretFound.visible = true;
  536.                     if(hitSecret)
  537.                     {
  538.                         points += 1000;
  539.                         hitSecret = false;
  540.                     }
  541.                 }
  542.             }
  543.            
  544.             else
  545.             {
  546.                 secretFound.visible = false;
  547.             }
  548.         }
  549.     }
  550. }
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586. //-------------------------------------------------------------------------------------------------
  587. //Program: PSP_Assignment2_Jon(s10105455k)_Shafi(s10087528k)
  588. //Created by: Jon Michael Jubelag & Adam Adil Shafi
  589. //Date Created: Week 13
  590. //Date Modified: 2nd Feb 2012
  591. //Description: The Assignment 2
  592. //------------------------------------------------------------------------------------------------
  593.  
  594. package
  595. {
  596.     import flash.events.*;
  597.     import flash.display.*;
  598.     import flash.ui.Keyboard;
  599.     import flash.utils.Dictionary;
  600.     import flash.utils.Timer;
  601.     import fl.transitions.Tween;
  602.     import fl.transitions.easing.*;
  603.     import fl.transitions.TweenEvent;
  604.     import flash.media.Sound;
  605.     import flash.media.SoundChannel;
  606.  
  607.     public class Player extends MovieClip
  608.     {
  609.         //Properties
  610.         private var main:MovieClip;//Store the main timeline
  611.        
  612.         private var dx:Number;
  613.         private var dy:Number;
  614.        
  615.         private var jump:Boolean;
  616.         private var jumping:Boolean;
  617.         private var falling:Boolean;
  618.         private var grounded:Boolean;
  619.        
  620.         private var moveLeft:Boolean;
  621.         private var moveRight:Boolean;
  622.        
  623.         private var platforms:Dictionary;
  624.         private var walls:Dictionary;
  625.         private var enemies:Dictionary;
  626.         private var hpDrops:Dictionary;
  627.         private var endPoints:Dictionary;
  628.        
  629.         private var yDiff:Number;
  630.        
  631.         private var clingTimer:Timer;
  632.         private var clingLeft:Boolean;
  633.         private var clingRight:Boolean;
  634.         private var jumpTimer:Timer;
  635.         private var inAirUp:Boolean;
  636.         private var inAirDown:Boolean;
  637.        
  638.         private var bullet:MovieClip; //To store the Bullet
  639.         private var shoot:Boolean; //To shoot bullet
  640.         private var dir:int; // direction for bullet
  641.        
  642.         private var hitBack:Boolean;
  643.         private var enemyDetect:Boolean;
  644.         private var enemyAlive:Boolean;
  645.        
  646.         private var hurt:Boolean;
  647.         private var vulnerable:Boolean;
  648.         private var invulTimer:Timer;
  649.         private var playerHealth:Number;
  650.        
  651.         private var gun357:Sound;
  652.         private var gun357Channel:SoundChannel;
  653.        
  654.         private var beep:Sound;
  655.         private var beepChannel:SoundChannel;
  656.        
  657.         private var shootTrue:Boolean;
  658.         private var shootTimer:Timer;
  659.  
  660.         public function Player(iMain:MovieClip)
  661.         {
  662.             //Constructor
  663.             this.dx = 10;
  664.             this.dy = 5;
  665.            
  666.             this.main = iMain;//pass main timeline into player class
  667.            
  668.             this.enemies = this.main.getEnemies();
  669.             this.walls = this.main.getWalls();
  670.             this.platforms = this.main.getPlatforms();//takes the private platform variable from GameControl.as and stores it into the platform variable in Player.as
  671.             this.hpDrops = this.main.getHpDrops();
  672.             this.endPoints = this.main.getEndPoints();
  673.            
  674.             this.main.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
  675.             this.main.stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
  676.             this.main.stage.addEventListener(KeyboardEvent.KEY_DOWN, jumpKeyHandler);
  677.             this.main.stage.addEventListener(KeyboardEvent.KEY_DOWN, shootKeyHandler);
  678.             this.main.stage.addEventListener(TweenEvent.MOTION_FINISH, tweenFinish);           
  679.            
  680.             gun357 = new gun1();
  681.             gun357Channel = new SoundChannel();
  682.             beep = new beep1();
  683.             beepChannel = new SoundChannel();
  684.            
  685.             clingTimer = new Timer(500,1);
  686.             clingTimer.addEventListener(TimerEvent.TIMER_COMPLETE, clingDone);
  687.            
  688.             jumpTimer = new Timer(450, 1);
  689.             jumpTimer.addEventListener(TimerEvent.TIMER_COMPLETE, jumpGap);
  690.            
  691.             this.dir = 1;
  692.             bullet = new Bullet(this); //Create a copy of bullet
  693.            
  694.             enemyDetect = false;
  695.             this.addEventListener(Event.ENTER_FRAME, detectPlayer);
  696.            
  697.             vulnerable = true;
  698.             hurt = false;
  699.             this.addEventListener(Event.ENTER_FRAME, playerHurt);
  700.             invulTimer = new Timer (1500, 1);
  701.             invulTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone);
  702.            
  703.             this.addEventListener(Event.ENTER_FRAME, getHp);
  704.             this.addEventListener(Event.ENTER_FRAME, endLevel);
  705.            
  706.             playerHealth = 5;
  707.         }
  708.  
  709. //-----------------------------------------------------------------------------------------------------------------------//
  710. //------------------------------------------------KEY HANDLER CODES------------------------------------------------------//
  711. //-----------------------------------------------------------------------------------------------------------------------//
  712.  
  713.         //Method: Key down handler;
  714.         private function keyDownHandler(evt:KeyboardEvent):void
  715.         {
  716.             if (evt.keyCode == Keyboard.LEFT)
  717.             {
  718.                 this.moveLeft = true;
  719.                 this.moveRight = false;
  720.                 this.scaleX = -1;
  721.                 this.dir = -1;
  722.             }
  723.  
  724.             else if (evt.keyCode == Keyboard.RIGHT)
  725.             {
  726.                 this.moveRight = true;
  727.                 this.moveLeft = false;
  728.                 this.scaleX = 1;
  729.                 this.dir = 1;
  730.             }
  731.         }
  732.        
  733.         //Method: Jump key Handler
  734.         private function jumpKeyHandler(evt:KeyboardEvent):void
  735.         {
  736.             if (evt.keyCode == Keyboard.SPACE)
  737.             {
  738.                 this.jump = true;
  739.                 this.main.stage.removeEventListener(KeyboardEvent.KEY_DOWN, jumpKeyHandler);
  740.                 jumpTimer.start();
  741.             }
  742.         }
  743.        
  744.         //Method: Shoot Key Handler
  745.         public function shootKeyHandler(evt:KeyboardEvent):void
  746.         {
  747.             if (evt.keyCode == 88)
  748.             {
  749.                 this.shoot = true;
  750.             }
  751.         }
  752.  
  753.         //Method: Key up handler
  754.         private function keyUpHandler(evt:KeyboardEvent):void
  755.         {
  756.             if (evt.keyCode == Keyboard.LEFT)
  757.             {
  758.                 this.moveLeft = false;
  759.             }
  760.  
  761.             else if (evt.keyCode == Keyboard.RIGHT)
  762.             {
  763.                 this.moveRight = false;
  764.             }
  765.  
  766.             else if (evt.keyCode == Keyboard.SPACE)
  767.             {
  768.                 this.jump = false;
  769.             }
  770.            
  771.             else if (evt.keyCode == 88)
  772.             {
  773.                 this.shoot = false;
  774.             }
  775.         }
  776.        
  777. //-----------------------------------------------------------------------------------------------------------------------//
  778. //------------------------------------------------JUMP/CLING TIMER CODES-------------------------------------------------//
  779. //-----------------------------------------------------------------------------------------------------------------------//
  780.                
  781.         //Method: Jump Gap
  782.         public function jumpGap(evt:TimerEvent):void //ensure that the player cannot hold down the jump button.
  783.         {
  784.             this.main.stage.addEventListener(KeyboardEvent.KEY_DOWN, jumpKeyHandler);
  785.             jumpTimer.reset();
  786.         }
  787.        
  788.         //Method: Tween Finish
  789.         public function tweenFinish(evt:Event):void
  790.         {
  791.             this.dy = 5;
  792.             this.dx = 10;
  793.         }
  794.        
  795.         //Method: Cling Timer
  796.         public function clingDone(evt:TimerEvent):void  //Pushes the player away from wall after timer is done.
  797.         {
  798.                 if (clingRight)
  799.                 {
  800.                     this.x += 15;
  801.                     clingRight = false;
  802.                     this.dy = 5;
  803.                     this.dx = 10;
  804.                 }
  805.                
  806.                 if (clingLeft)
  807.                 {
  808.                     this.x -= 15;
  809.                     clingLeft = false;
  810.                     this.dy = 5;
  811.                     this.dx = 10;
  812.                 }
  813.                 clingTimer.reset();
  814.         }
  815.  
  816. //-----------------------------------------------------------------------------------------------------------------------//
  817. //------------------------------------------------BULLET CODE------------------------------------------------------------//
  818. //-----------------------------------------------------------------------------------------------------------------------//
  819.        
  820.        
  821.         //Method: Shoot Bullet
  822.         public function shootBullet():void
  823.         {
  824.             if (shoot && !clingLeft && !clingRight)//player cannot shoot while clinging
  825.             {
  826.                 if(!bullet.getIsMoving()) //Check if bullet is fired
  827.                 {
  828.                     gun357.play();
  829.                     hitBack = true;
  830.                    
  831.                     //Spawn the bullet
  832.                     bullet.x = this.x + (50 * dir);
  833.                     bullet.y = this.y - this.height/2;
  834.                    
  835.                     this.main.addChild(bullet);
  836.                     this.addEventListener(Event.ENTER_FRAME, bulletHit);
  837.                     bullet.shoot(dir);
  838.                 }
  839.                
  840.                 if(!moveLeft && !moveRight && !hurt)
  841.                 {
  842.                     this.gotoAndPlay(5);
  843.                 }
  844.                
  845.                 if(moveLeft || moveRight && !hurt)
  846.                 {
  847.                     this.gotoAndPlay(7);
  848.                 }
  849.             }
  850.        
  851.         }
  852.  
  853.    
  854.         // Method: Remove Bullet
  855.         public function removeBullet():void
  856.         {
  857.             this.main.removeChild(bullet);
  858.         }
  859.        
  860.         //Method: Bullet Hit
  861.         public function bulletHit(e:Event):void //Makes sure bullets cant go through walls
  862.         {
  863.             for (var wall in this.walls)
  864.             {
  865.                 if (this.bullet.hitTestObject(wall.hitBoxWallLeft) || this.bullet.hitTestObject(wall.hitBoxWallRight))
  866.                 {
  867.                     bullet.y = 1000;
  868.                 }
  869.             }
  870.            
  871.             for (var pf in this.platforms) //Makes sure bullets cant go through platforms
  872.             {
  873.                 if (this.bullet.hitTestObject(pf.hitBoxPfLeftSide) || this.bullet.hitTestObject(pf.hitBoxPfRightSide))
  874.                 {
  875.                     bullet.y = 1000;
  876.                 }
  877.             }
  878.            
  879.             for (var en in this.enemies)
  880.             {
  881.                 if (this.bullet.hitTestObject(en.enemyBody))
  882.                 {
  883.                     en.minusEnemyHealth()
  884.                    
  885.                     if(en.getHealth() == 0)
  886.                     {
  887.                         en.gotoAndPlay(7);
  888.                         delete enemies[en];
  889.                     }
  890.                     bullet.y = 1000;
  891.                 }
  892.                
  893.                 if (this.bullet.hitTestObject(en.enemyBack) && hitBack) //checks if bullet hits the back of enemy.
  894.                 {
  895.                     en.enemyBackHit();
  896.                     hitBack = false;
  897.                 }
  898.             }
  899.         }
  900.        
  901. //-----------------------------------------------------------------------------------------------------------------------//
  902. //------------------------------------------------MOVEMENT CODE----------------------------------------------------------//
  903. //-----------------------------------------------------------------------------------------------------------------------//
  904.        
  905.        
  906.         //Method: Move Player
  907.         public function movePlayer():void
  908.         {
  909.             {
  910.                 this.y +=  this.dy;//move vertically
  911.  
  912.                 if (! jumping && ! falling)
  913.                 {
  914.                     this.dy = 5;
  915.                     this.inAirDown = false;
  916.                     this.falling = true;
  917.                 }
  918.  
  919.                 if (moveLeft) //equivalent to if(moveLeft==true)
  920.                 {
  921.                     this.x -=  this.dx;
  922.                     if (moveLeft && this.x <= 400)
  923.                     {
  924.                         this.dx = 0;
  925.                         main.getChildByName("level").x += 10;
  926.                     }
  927.                     else
  928.                     {
  929.                         this.dx = 10;
  930.                     }
  931.                    
  932.                     if(!shoot)
  933.                     {
  934.                         this.gotoAndPlay(1);
  935.                     }
  936.                    
  937.                 }
  938.                
  939.                 if (moveRight)
  940.                 {
  941.                     this.x +=  this.dx;
  942.                     if (moveRight && this.x >= 500)
  943.                     {
  944.                         this.dx = 0;
  945.                         main.getChildByName("level").x -= 10;
  946.                     }
  947.                     else
  948.                     {
  949.                         this.dx = 10;
  950.                     }
  951.                    
  952.                     if(!shoot)
  953.                     {
  954.                         this.gotoAndPlay(1);
  955.                     }
  956.                    
  957.                    
  958.                 }
  959.                
  960.                 if (!moveRight && !moveLeft && !jump && !shoot && !jumping && !hurt)// && !clingLeft && !clingRight && grounded)
  961.                 {
  962.                     this.gotoAndPlay(3);
  963.                 }
  964.                
  965.                 if (hurt)
  966.                 {
  967.                     this.gotoAndPlay(15);
  968.                 }
  969.                
  970.                 if (!jumping && inAirDown && !grounded)
  971.                 {
  972.                     this.gotoAndPlay(11);
  973.                 }
  974.                
  975.                 if (jump && grounded)
  976.                 {
  977.                     this.dy -=  50;//Jump power
  978.                     jump = false
  979.                     this.jumping = true;
  980.                     this.grounded = false;
  981.                     this.falling = false;
  982.                     inAirDown = false;
  983.                 }
  984.                
  985.                 if (jump && clingLeft)
  986.                 {
  987.                     clingLeft = false;
  988.                     jump = false;
  989.                     this.dy = 5;
  990.                     this.dx = 10;
  991.                     new Tween (this, 'x', Regular.easeOut, this.x, this.x -= 55, 8, false);
  992.                     new Tween (this, 'y', Regular.easeOut, this.y, this.y - 110, 8, false);
  993.                     clingTimer.reset();
  994.                 }
  995.                
  996.                 if (jump && clingRight)
  997.                 {
  998.                     clingRight = false;
  999.                     jump = false
  1000.                     this.dy = 5;
  1001.                     this.dx = 10;
  1002.                     new Tween (this, 'x', Regular.easeOut, this.x, this.x + 55, 8, false);
  1003.                     new Tween (this, 'y', Regular.easeOut, this.y, this.y - 110, 8, false);
  1004.                     clingTimer.reset();
  1005.                 }
  1006.                
  1007.                 if (jumping)
  1008.                 {
  1009.                     if(!inAirDown)
  1010.                     {
  1011.                         this.gotoAndPlay(9);
  1012.                     }
  1013.                     this.dy *=  0.8;//Slow it down
  1014.                     if (this.dy > -1)
  1015.                     {
  1016.                         this.dy = 5;
  1017.                         falling = true;
  1018.                         jumping = false;
  1019.                         this.inAirDown = true;
  1020.                     }
  1021.                 }
  1022.  
  1023.                 if (falling)
  1024.                 {
  1025.                     this.dy *=  1.18;
  1026.                     grounded = false;
  1027.                     if (dy > 35)
  1028.                     {
  1029.                         dy = 35;
  1030.                     }
  1031.                 }
  1032.  
  1033.                
  1034.                 //--------------//
  1035.                 //  Platforms   //
  1036.                 //--------------//
  1037.  
  1038.                 //to check if it his the platform
  1039.                 // pf is the object key to point to each item in the dictionary.
  1040.                 for (var pf in this.platforms)
  1041.                 {
  1042.                     if (this.hitBoxPlayer.hitTestObject(pf.hitBoxPfTop) && falling)
  1043.                     {
  1044.                         this.dy = 0;//set vertical speed to 0
  1045.                         this.grounded = true;
  1046.                         this.falling = false;
  1047.                         this.inAirDown = false;
  1048.                         this.y = pf.y + main.getYDiff();
  1049.                     }
  1050.  
  1051.                     if (this.hitTestObject(pf.hitBoxPfBottom))
  1052.                     {
  1053.                         this.y +=  15; //pushes player away
  1054.                         this.dy = 15;
  1055.                     }
  1056.  
  1057.                     if (this.hitTestObject(pf.hitBoxPfLeftSide))
  1058.                     {
  1059.                         this.x -= 8;
  1060.                     }
  1061.  
  1062.                     if (this.hitTestObject(pf.hitBoxPfRightSide))
  1063.                     {
  1064.                         this.x += 8;
  1065.                     }
  1066.                 }
  1067.  
  1068.                 //-----------//
  1069.                 //   Walls   //
  1070.                 //-----------//
  1071.  
  1072.                 for (var wall in this.walls)
  1073.                 {
  1074.                     if (this.playerBody.hitTestObject(wall.hitBoxWallLeft))
  1075.                     {
  1076.                         this.x = wall.x - 45 + main.getChildByName("level").x;
  1077.                         if (jumping || falling)
  1078.                         {
  1079.                             clingLeft = true;
  1080.                         }
  1081.                     }
  1082.  
  1083.                     if (this.playerBody.hitTestObject(wall.hitBoxWallRight))
  1084.                     {
  1085.                         this.x = wall.x + 45 + main.getChildByName("level").x;
  1086.                         if (jumping || falling)
  1087.                         {
  1088.                             clingRight = true;
  1089.                         }
  1090.                     }
  1091.                 }
  1092.                
  1093.                 if (clingLeft)
  1094.                 {
  1095.                     clingTimer.start();
  1096.                     this.dy = 0;
  1097.                     this.dx = 1;
  1098.                     this.scaleX = -1;
  1099.                     this.gotoAndPlay(13);
  1100.                 }
  1101.                
  1102.                 if (clingRight)
  1103.                 {
  1104.                     clingTimer.start();
  1105.                     this.dy = 0;
  1106.                     this.dy = 1;
  1107.                     this.scaleX = 1;
  1108.                     this.gotoAndPlay(13);
  1109.                 }
  1110.             }  
  1111.         }
  1112.        
  1113. //-----------------------------------------------------------------------------------------------------------------------//
  1114. //------------------------------------------------DETECTION CODE---------------------------------------------------------//
  1115. //-----------------------------------------------------------------------------------------------------------------------//
  1116.        
  1117.         //Method: Detect Player
  1118.         public function detectPlayer(e:Event):void
  1119.         {
  1120.             for (var en in this.enemies)
  1121.             {
  1122.                 if (this.hitTestObject(en.enemySight))
  1123.                 {
  1124.                     enemyDetect = true;
  1125.                     en.playerDetected();
  1126.                 }
  1127.                
  1128.                 else
  1129.                 {
  1130.                     en.playerUnDetected();
  1131.                 }
  1132.             }
  1133.         }
  1134.        
  1135. //-----------------------------------------------------------------------------------------------------------------------//
  1136. //------------------------------------------------PLAYER HURT CODE-------------------------------------------------------//
  1137. //-----------------------------------------------------------------------------------------------------------------------//
  1138.  
  1139.         //Method: Player Hurt
  1140.         public function playerHurt(e:Event):void
  1141.         {
  1142.             for (var en in this.enemies)
  1143.             {
  1144.                 if (this.playerBody.hitTestObject(en.enemyBody) && vulnerable)
  1145.                 {
  1146.                     playerHealth -= 1;
  1147.                     vulnerable = false;
  1148.                     invulTimer.start();
  1149.                     main.minusHealth();
  1150.                     main.checkHealth();
  1151.                     hurt = true;
  1152.                 }
  1153.                
  1154.                 else
  1155.                 {
  1156.                     hurt = false; //hurt is used to control certain animations
  1157.                 }
  1158.             }
  1159.         }
  1160.        
  1161.         //Method: invul Timer Done
  1162.         public function timerDone(evt:TimerEvent):void //the invul timer prevents players from getting hurt too fast.
  1163.         {
  1164.             vulnerable = true;
  1165.             invulTimer.reset();
  1166.         }
  1167.        
  1168.  
  1169. //-----------------------------------------------------------------------------------------------------------------------//
  1170. //------------------------------------------------GET HP DROP CODE-------------------------------------------------------//
  1171. //-----------------------------------------------------------------------------------------------------------------------//
  1172.    
  1173.         //Method: get HP
  1174.         public function getHp(evt:Event):void
  1175.         {
  1176.             for (var hp in this.hpDrops)
  1177.             {
  1178.                 if (this.hitTestObject(hp) && (this.playerHealth < 5))
  1179.                 {
  1180.                     beep.play();
  1181.                     playerHealth += 1;
  1182.                     main.addHealth();
  1183.                     hp.visible = false;
  1184.                     delete hpDrops[hp];
  1185.                     break;
  1186.                 }
  1187.             }
  1188.         }
  1189.        
  1190.         public function endLevel(evt:Event):void
  1191.         {
  1192.             for (var eP in this.endPoints)
  1193.             {
  1194.                 if (this.hitTestObject(eP))
  1195.                 {
  1196.                     main.levelEnd();
  1197.                 }
  1198.             }
  1199.         }
  1200.     }
  1201. }
  1202.  
  1203.  
  1204.  
  1205.  
  1206.  
  1207.  
  1208.  
  1209.  
  1210.  
  1211.  
  1212.  
  1213.  
  1214.  
  1215.  
  1216.  
  1217.  
  1218.  
  1219.  
  1220.  
  1221.  
  1222.  
  1223.  
  1224.  
  1225.  
  1226.  
  1227.  
  1228.  
  1229.  
  1230.  
  1231.  
  1232.  
  1233.  
  1234.  
  1235.  
  1236.  
  1237.  
  1238.  
  1239. //-------------------------------------------------------------------------------------------------
  1240. //Program: PSP_Assignment2_Jon(s10105455k)_Shafi(s10087528k)
  1241. //Created by: Jon Michael Jubelag & Adam Adil Shafi
  1242. //Date Created: Week 13
  1243. //Date Modified: 2nd Feb 2012
  1244. //Description: The Assignment 2
  1245. //------------------------------------------------------------------------------------------------
  1246.  
  1247. package  
  1248. {
  1249.    
  1250.     import flash.utils.Timer;
  1251.     import flash.display.*;
  1252.     import flash.events.*;
  1253.     import flash.utils.Dictionary;
  1254.     import flash.media.Sound;
  1255.     import flash.media.SoundChannel;   
  1256.    
  1257.     public class Enemy extends MovieClip
  1258.     {
  1259.        
  1260.         //Properties
  1261.         private var main:MovieClip;
  1262.         private var player:MovieClip;
  1263.         private var yDiff:Number;
  1264.        
  1265.         private var dir:int; //Direction in which the enemy moves.
  1266.         private var dirChange:Boolean;
  1267.        
  1268.         private var dx:Number; //horizontal speed
  1269.         private var dy:Number; //vertical speed
  1270.        
  1271.         private var platforms:Dictionary;
  1272.         private var walls:Dictionary;
  1273.        
  1274.         private var enemyHp:Number;
  1275.         private var enemyAlive:Boolean;
  1276.        
  1277.         private var enemyHitSound:Sound;
  1278.         private var enemyDieSound:Sound;
  1279.        
  1280.         private var enemyHitSoundChannel:SoundChannel;
  1281.         private var enemyDieSoundChannel:SoundChannel;
  1282.        
  1283.         public function Enemy()
  1284.         {
  1285.             // constructor code
  1286.             this.dir = 1;
  1287.             this.dx = 5;
  1288.             this.dy = 10;
  1289.             this.addEventListener(Event.ENTER_FRAME, moveEnemy);
  1290.             this.addEventListener(Event.ENTER_FRAME, changeDir);
  1291.             this.addEventListener(Event.ENTER_FRAME, changeDirWall);
  1292.             dirChange = true;
  1293.            
  1294.             this.enemyHp = 5;
  1295.             enemyAlive = true;
  1296.            
  1297.             enemyHitSound = new eHit();
  1298.             enemyHitSoundChannel = new SoundChannel();
  1299.            
  1300.             enemyDieSound = new eDeath();
  1301.             enemyHitSoundChannel = new SoundChannel();
  1302.         }
  1303.  
  1304. //-----------------------------------------------------------------------------------------------------------------------//
  1305. //---------------------------------------------------SET MAIN CODE-------------------------------------------------------//
  1306. //-----------------------------------------------------------------------------------------------------------------------//
  1307.  
  1308.         //Method: Set Main
  1309.         public function toMain(iMain:MovieClip):void
  1310.         {
  1311.             this.main = iMain;
  1312.             this.platforms = this.main.getPlatforms();
  1313.             this.walls = this.main.getWalls();
  1314.         }
  1315.                
  1316. //-----------------------------------------------------------------------------------------------------------------------//
  1317. //------------------------------------------------PLAYER DETECTION CODE--------------------------------------------------//
  1318. //-----------------------------------------------------------------------------------------------------------------------//            
  1319.                
  1320.         //Method: Player Detected
  1321.         public function playerDetected() //enemy runs faster if player is in sight
  1322.         {
  1323.             this.dx = 15;
  1324.             dirChange = false;
  1325.             if(enemyAlive)
  1326.             {
  1327.                 this.gotoAndPlay(3);
  1328.             }
  1329.         }
  1330.        
  1331.         public function playerUnDetected() //reverts back to normal speed
  1332.         {
  1333.             this.dx = 5;
  1334.             dirChange = true;
  1335.             if(enemyAlive)
  1336.             {
  1337.                 this.gotoAndPlay(1);
  1338.             }
  1339.         }
  1340.        
  1341. //-----------------------------------------------------------------------------------------------------------------------//
  1342. //-------------------------------------------------MOVE ENEMY CODE-------------------------------------------------------//
  1343. //-----------------------------------------------------------------------------------------------------------------------//    
  1344.        
  1345.         //Method: Move Enemy
  1346.         private function moveEnemy(e:Event)
  1347.         {
  1348.             changeSightLength()
  1349.             this.y += this.dy;
  1350.            
  1351.             this.x += this.dx * dir;
  1352.        
  1353.             for (var pf in this.platforms)
  1354.             {
  1355.                 if (this.enemyBody.hitTestObject(pf.hitBoxPfTop))
  1356.                 {
  1357.                     this.dy = 0;//set vertical speed to 0
  1358.                     this.y = pf.y;
  1359.                 }
  1360.            
  1361.                 else
  1362.                 {
  1363.                     this.dy = 10;
  1364.                 }
  1365.             }
  1366.         }
  1367.        
  1368. //-----------------------------------------------------------------------------------------------------------------------//
  1369. //-------------------------------------------------DIRECTION CODES-------------------------------------------------------//
  1370. //-----------------------------------------------------------------------------------------------------------------------//    
  1371.        
  1372.         //Method: Change Enemy Direction
  1373.         //Changes the direction in which the enemy faces and moves when it hits a wall or the end of a platform
  1374.         public function changeDir(e:Event)
  1375.         {
  1376.             for (var pf in this.platforms)
  1377.             {
  1378.                 if (this.enemyBody.hitTestObject(pf.hitBoxEnd1) && dirChange)
  1379.                 {
  1380.                     dir = dir * -1;
  1381.                     this.scaleX *= -1;
  1382.                     this.enemySight.width = 360;
  1383.                     this.x += pf.hitBoxEnd1.width;
  1384.                 }
  1385.                
  1386.                 else if (this.enemyBody.hitTestObject(pf.hitBoxEnd2) && dirChange)
  1387.                 {
  1388.                     dir = dir * -1;
  1389.                     this.scaleX *= -1;
  1390.                     this.enemySight.width = 360;
  1391.                     this.x -= pf.hitBoxEnd1.width;
  1392.                 }
  1393.             }
  1394.         }
  1395.        
  1396.         public function changeDirWall(e:Event)
  1397.         {
  1398.             for (var wall in this.walls)
  1399.             {
  1400.                 if (this.enemyBody.hitTestObject(wall.hitBoxWallLeft))
  1401.                 {
  1402.                     dir = dir * -1;
  1403.                     this.scaleX *= -1;
  1404.                     this.enemySight.width = 360;
  1405.                     this.x -= wall.hitBoxWallLeft.width;
  1406.                 }
  1407.                
  1408.                 if (this.enemyBody.hitTestObject(wall.hitBoxWallRight))
  1409.                 {
  1410.                     dir = dir * -1;
  1411.                     this.scaleX *= -1;
  1412.                     this.enemySight.width = 360;
  1413.                     this.x += wall.hitBoxWallLeft.width;
  1414.                 }
  1415.             }
  1416.         }
  1417.        
  1418. //-----------------------------------------------------------------------------------------------------------------------//
  1419. //------------------------------------------------ENEMY SIGHT CODE-------------------------------------------------------//
  1420. //-----------------------------------------------------------------------------------------------------------------------//
  1421.  
  1422.         //Method: Change Enemy Sight Length
  1423.         //Reduces the size of the enemy sights so it wont see through the walls.
  1424.         public function changeSightLength()
  1425.         {
  1426.             for (var wall in this.walls)
  1427.             {
  1428.                 if (this.enemySight.hitTestObject(wall.hitBoxWallRight))
  1429.                 {
  1430.                     this.enemySight.width = this.x - wall.x;
  1431.                 }
  1432.                
  1433.                 else if (this.enemySight.hitTestObject(wall.hitBoxWallLeft))
  1434.                 {
  1435.                     this.enemySight.width = wall.x - this.x;
  1436.                 }
  1437.             }
  1438.         }
  1439.        
  1440. //-----------------------------------------------------------------------------------------------------------------------//
  1441. //------------------------------------------------ENEMY HEALTH CODES-----------------------------------------------------//
  1442. //-----------------------------------------------------------------------------------------------------------------------//    
  1443.        
  1444.         //Method: Minus Enemy Health
  1445.         public function minusEnemyHealth()
  1446.         {
  1447.             enemyHp -= 1;
  1448.             if (enemyHp > 0)
  1449.             {
  1450.                 this.gotoAndPlay(5);
  1451.                 enemyHitSound.play();
  1452.             }
  1453.            
  1454.             else if (enemyHp == 0)
  1455.             {
  1456.                 enemyAlive = false;
  1457.                 this.removeEventListener(Event.ENTER_FRAME, moveEnemy);
  1458.                 this.main.addPoints();
  1459.                 enemyDieSound.play();
  1460.             }
  1461.         }
  1462.        
  1463.         public function getHealth():Number
  1464.         {
  1465.             return this.enemyHp;
  1466.         }
  1467.        
  1468.         //Method: Change Direction when hit
  1469.         public function enemyBackHit() //flips the enemy if its hit in the back
  1470.         {
  1471.             if(enemyAlive)
  1472.             {
  1473.                 dirChange = false;
  1474.                 dir = dir * -1;
  1475.                 this.scaleX *= -1;
  1476.                 this.enemySight.width = 360;
  1477.            
  1478.                 this.gotoAndPlay(5);
  1479.             }
  1480.         }
  1481.        
  1482.         public function getEnemyAlive():Boolean
  1483.         {
  1484.             return this.enemyAlive;
  1485.         }
  1486.     }
  1487. }
  1488.  
  1489.  
  1490.  
  1491.  
  1492.  
  1493.  
  1494.  
  1495.  
  1496.  
  1497.  
  1498.  
  1499.  
  1500.  
  1501.  
  1502.  
  1503.  
  1504.  
  1505.  
  1506.  
  1507.  
  1508.  
  1509.  
  1510.  
  1511.  
  1512.  
  1513.  
  1514.  
  1515.  
  1516.  
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522.  
  1523.  
  1524.  
  1525. //-------------------------------------------------------------------------------------------------
  1526. //Program: PSP_Assignment2_Jon(s10105455k)_Shafi(s10087528k)
  1527. //Created by: Jon Michael Jubelag & Adam Adil Shafi
  1528. //Date Created: Week 13
  1529. //Date Modified: 2nd Feb 2012
  1530. //Description: The Assignment 2
  1531. //------------------------------------------------------------------------------------------------
  1532.  
  1533. package  
  1534. {
  1535.    
  1536.     import flash.utils.Timer;
  1537.     import flash.events.*;
  1538.     import flash.display.*;
  1539.     import flash.utils.Dictionary;
  1540.    
  1541.     public class Bullet extends MovieClip
  1542.     {
  1543.        
  1544.         //Properties
  1545.         private var main:MovieClip;
  1546.         private var dir:int; //-1 means left, 1 means right
  1547.         private var speed:Number; //speed
  1548.         private var accel:Number; //acceleration
  1549.        
  1550.         private var moveTimer:Timer;
  1551.         private var lagTimer:Timer
  1552.        
  1553.         private var isMoving:Boolean;
  1554.        
  1555.         private var player:MovieClip;
  1556.        
  1557.         public function Bullet(iParent:MovieClip)
  1558.         {
  1559.             // constructor code
  1560.             this.dir = 1;
  1561.             this.speed = 20;
  1562.             this.accel = 1.2;
  1563.             this.isMoving = false
  1564.             player = iParent;
  1565.            
  1566.             moveTimer = new Timer (20, 0);
  1567.             moveTimer.addEventListener(TimerEvent.TIMER, moveTimerHandler);
  1568.            
  1569.             lagTimer = new Timer (700,1); //1 means run once only
  1570.             lagTimer.addEventListener(TimerEvent.TIMER_COMPLETE, lagTimerCompleteHandler);
  1571.         }
  1572.        
  1573. //-----------------------------------------------------------------------------------------------------------------------//
  1574. //------------------------------------------------TIMER HANDLER CODES----------------------------------------------------//
  1575. //-----------------------------------------------------------------------------------------------------------------------//
  1576.        
  1577.         //Method: moveTimer Handler
  1578.         private function moveTimerHandler(evt:TimerEvent):void
  1579.         {
  1580.             this.x += this.speed * accel * dir;
  1581.         }
  1582.        
  1583.         //Method: lagTimer Complete Handler
  1584.         private function lagTimerCompleteHandler(evt:TimerEvent):void
  1585.         {
  1586.             isMoving = false;
  1587.             lagTimer.reset();
  1588.             moveTimer.stop();
  1589.             player.removeBullet();
  1590.         }
  1591.        
  1592. //-----------------------------------------------------------------------------------------------------------------------//
  1593. //-----------------------------------------------SHOOT BULLET CODE-------------------------------------------------------//
  1594. //-----------------------------------------------------------------------------------------------------------------------//    
  1595.  
  1596.         //Method: Shoot Bullet
  1597.         public function shoot(iDir:int):void
  1598.         {
  1599.             if (!isMoving)
  1600.             {
  1601.                 dir = iDir;
  1602.                 isMoving = true;
  1603.                 moveTimer.start();
  1604.                 lagTimer.start();
  1605.             }
  1606.         }
  1607.        
  1608. //-----------------------------------------------------------------------------------------------------------------------//
  1609. //--------------------------------------------------IS MOVING CODE-------------------------------------------------------//
  1610. //-----------------------------------------------------------------------------------------------------------------------//    
  1611.        
  1612.         //Method: Get isMoving properties
  1613.         public function getIsMoving():Boolean
  1614.         {
  1615.             return this.isMoving;
  1616.         }
  1617.     }
  1618. }
Add Comment
Please, Sign In to add comment