Guest User

Untitled

a guest
Apr 17th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import flash.events.KeyboardEvent;
  2. import flash.events.Event;
  3. import flash.geom.Point;
  4.  
  5. //Onda/Goda objekt OSV
  6. var goodieAmount:int =25;
  7. var allGoodies:Array = new Array();
  8. var badieAmount:int = 10;
  9. var allBadies:Array = new Array();
  10. //Piltangenter OSV
  11. var leftArrow:Boolean = false;
  12. var rightArrow:Boolean = false;
  13. var upArrow:Boolean = false;
  14. var downArrow:Boolean = false;
  15. var shootNow:Boolean = false;
  16. //Skepparvariabler OSV
  17. var shipSpeed:Number = 0;
  18. var shipAccel:Number = 0.002;
  19. var shipDecel:Number = 0.0003;
  20. var maxSpeed:Number = 0.3;
  21. var shipRot:Number = 0;
  22. var turnSpeed:Number = 0.4;
  23. var newY:int;
  24. var newX:int;
  25. var lastTime:int = 0;
  26. var myShip:shipMC = new shipMC();
  27. myShip.x = 400;
  28. myShip.y = 300;
  29. addChild(myShip);
  30. var allShots:Array = new Array;
  31. var point = 0;
  32. var antalliv = 5;
  33. score.text = "Score "
  34. lives.text ="Liv " + antalliv;
  35.  
  36. //Lyssnare OSV
  37.  
  38. stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
  39. stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
  40. this.addEventListener(Event.ENTER_FRAME,appLoop);
  41. this.addEventListener(Event.ENTER_FRAME,objectLoopGood);
  42. this.addEventListener(Event.ENTER_FRAME,objectLoopBad);
  43.  
  44. //Funktioner
  45. //Initieringsfunktion goda/onda objekt
  46. function initGoodies():void {
  47.     for(var i:int=0;i<goodieAmount;i++){
  48.         var myGoodie:Goodie = new Goodie();
  49.         myGoodie.x = 50 + Math.floor(700*Math.random());
  50.         myGoodie.y = 50 + Math.floor(500*Math.random());
  51.         myGoodie.dx = 5 - Math.floor(10*Math.random());
  52.         myGoodie.dy = 5 - Math.floor(10*Math.random());
  53.         allGoodies.push(myGoodie);
  54.         addChild(myGoodie);
  55.     }
  56. }
  57.  
  58. function initBadies():void {
  59.     for(var i:int=0;i<badieAmount;i++){
  60.         var myBadie:Badie = new Badie();
  61.         myBadie.x = 50 + Math.floor(700*Math.random());
  62.         myBadie.y = 50 + Math.floor(500*Math.random());
  63.         myBadie.dx = 5 - Math.floor(10*Math.random());
  64.         myBadie.dy = 5 - Math.floor(10*Math.random());
  65.         allBadies.push(myBadie);
  66.         addChild(myBadie);
  67.     }
  68. }
  69.  
  70. function keyPressedDown(event:KeyboardEvent) {
  71.     if (event.keyCode == 37) {
  72.         leftArrow = true;
  73.     } else if (event.keyCode == 39) {
  74.         rightArrow = true;
  75.     } else if (event.keyCode == 38) {
  76.         upArrow = true;
  77.     } else if (event.keyCode == 40) {
  78.         downArrow = true;
  79.     } else if (event.keyCode == 32) {
  80.         shootNow = true;
  81.     }
  82. }
  83.  
  84. function keyPressedUp(event:KeyboardEvent) {
  85.     if (event.keyCode == 37) {
  86.         leftArrow = false;
  87.     } else if (event.keyCode == 39) {
  88.         rightArrow = false;
  89.     } else if (event.keyCode == 38) {
  90.         upArrow = false;
  91.     } else if (event.keyCode == 40) {
  92.         downArrow = false;
  93.     } else if (event.keyCode == 32) {
  94.         shootNow = false;
  95.     }
  96. }
  97.  
  98. function objectLoopGood(event:Event) {
  99.     for (var j:int= 0;j<allGoodies.length;j++) {
  100.         var newX:Number = allGoodies[j].x + allGoodies[j].dx;
  101.         var newY:Number = allGoodies[j].y + allGoodies[j].dy;
  102.        
  103.         if(newX<allGoodies[j].width/2) {
  104.             allGoodies[j].x = allGoodies[j].width/2 + (allGoodies[j].width/2 - newX);
  105.             allGoodies[j].dx *= -1;
  106.         }
  107.         else if(newX > (800 - allGoodies[j].width/2)) {
  108.             allGoodies[j].x = (800 - allGoodies[j].width/2) - (newX - (800 - allGoodies[j].width/2));
  109.             allGoodies[j].dx *= -1;
  110.         }
  111.         else {
  112.             allGoodies[j].x = newX;
  113.         }
  114.         //flytta i Y-led
  115.         if(newY<allGoodies[j].height/2) {
  116.             allGoodies[j].y = allGoodies[j].height/2 + (allGoodies[j].height/2 - newY);
  117.             allGoodies[j].dy *= -1;
  118.         }
  119.         else if(newY > (600 - allGoodies[j].height/2)) {
  120.             allGoodies[j].y = (600 - allGoodies[j].height/2) - (newY - (600 - allGoodies[j].height/2));
  121.             allGoodies[j].dy *= -1;
  122.         }
  123.         else {
  124.             allGoodies[j].y = newY;
  125.         }
  126.        
  127.         //Testa krock med skepp
  128.         var hitDist:Number = (myShip.width/2 + allGoodies[j].width/2);
  129.         var dist:Number = Point.distance(new Point(myShip.x,myShip.y), new Point(allGoodies[j].x,allGoodies[j].y));
  130.         if (dist<hitDist)
  131.         {
  132.             removeChild(allGoodies[j]);
  133.             allGoodies.splice(j,1);
  134.             point+=50;
  135.             score.text = "Score " + point;
  136.         }
  137.     }
  138. }
  139.            
  140. function objectLoopBad(event:Event) {
  141.     for (var j:int= 0;j<allBadies.length;j++) {
  142.         var newX:Number = allBadies[j].x + allBadies[j].dx;
  143.         var newY:Number = allBadies[j].y + allBadies[j].dy;
  144.        
  145.         if(newX<allBadies[j].width/2) {
  146.             allBadies[j].x = allBadies[j].width/2 + (allBadies[j].width/2 - newX);
  147.             allBadies[j].dx *= -1;
  148.         }
  149.         else if(newX > (800 - allBadies[j].width/2)) {
  150.             allBadies[j].x = (800 - allBadies[j].width/2) - (newX - (800 - allBadies[j].width/2));
  151.             allBadies[j].dx *= -1;
  152.         }
  153.         else {
  154.             allBadies[j].x = newX;
  155.         }
  156.         //flytta i Y-led
  157.         if(newY<allBadies[j].height/2) {
  158.             allBadies[j].y = allBadies[j].height/2 + (allBadies[j].height/2 - newY);
  159.             allBadies[j].dy *= -1;
  160.         }
  161.         else if(newY > (600 - allBadies[j].height/2)) {
  162.             allBadies[j].y = (600 - allBadies[j].height/2) - (newY - (600 - allBadies[j].height/2));
  163.             allBadies[j].dy *= -1;
  164.         }
  165.         else {
  166.             allBadies[j].y = newY;
  167.         }
  168.            
  169.         //Testa krock med skepp
  170.         var hitDist:Number = (myShip.width/2 + allBadies[j].width/2);
  171.         var dist:Number = Point.distance(new Point(myShip.x,myShip.y), new Point(allBadies[j].x,allBadies[j].y));
  172.         if (dist<hitDist)
  173.         {
  174.             removeChild(allBadies[j]);
  175.             allBadies.splice(j,1);
  176.             point-=25;
  177.             antalliv-=1;
  178.             score.text = "Score " + point;
  179.             lives.text = "Liv " + antalliv;
  180.             if (antalliv<1)
  181.                 {
  182.                     stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
  183.                     stage.removeEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
  184.                     this.removeEventListener(Event.ENTER_FRAME,appLoop);
  185.                     this.removeEventListener(Event.ENTER_FRAME,objectLoopGood);
  186.                     this.removeEventListener(Event.ENTER_FRAME,objectLoopBad);
  187.                     gameover1.text = "Game ";
  188.                     gameover2.text = "Over!";
  189.                 }
  190.         }
  191.        
  192.         for (var t:int= 0;t<allShots.length;t++)
  193.         {
  194.             var hitbadDist:Number = (allShots[t].width/2 + allBadies[j].width/2);
  195.             var badDist:Number = Point.distance(new Point(allShots[t].x,allShots[t].y), new Point(allBadies[j].x,allBadies[j].y));
  196.             if (badDist<hitbadDist)
  197.             {
  198.                 removeChild(allBadies[j]);
  199.                 allBadies.splice(j,1);
  200.                 removeChild(allShots[t])
  201.                 allShots.splice(t,1);
  202.                 point+=25;
  203.                 score.text = "Score " + point;
  204.                 lives.text = "Liv " + antalliv;
  205.             }
  206.         }
  207.         }
  208. }
  209.  
  210. function appLoop(event:Event)
  211. {
  212.     if (lastTime ==0)
  213.     {
  214.         lastTime = getTimer();
  215.     }
  216.     var timeDiff:int = getTimer()-lastTime;
  217.     lastTime += timeDiff;
  218.     //Höger/Vänsterpilar
  219.     if (leftArrow)
  220.     {
  221.         myShip.rotation -=(shipSpeed+0.1)*turnSpeed*timeDiff;
  222.     }
  223.     if (rightArrow)
  224.     {
  225.         myShip.rotation +=(shipSpeed+0.1)*turnSpeed*timeDiff;
  226.     }
  227.     //Upp/Ned
  228.     if (upArrow)
  229.     {
  230.         shipSpeed += shipAccel*timeDiff;
  231.         if (shipSpeed > maxSpeed)
  232.         {
  233.             shipSpeed = maxSpeed;
  234.         }
  235.     }
  236.     else if (downArrow)
  237.     {
  238.         shipSpeed -= shipAccel*timeDiff;
  239.         if (shipSpeed < maxSpeed)
  240.         {
  241.             shipSpeed = -maxSpeed;
  242.         }
  243.     }
  244.     else if (shipSpeed > 0)
  245.     {
  246.         shipSpeed -= shipDecel*timeDiff;
  247.         if (shipSpeed < 0)
  248.         {
  249.             shipSpeed = 0;
  250.         }
  251.     }
  252.     else if (shipSpeed < 0)
  253.     {
  254.         shipSpeed += shipDecel*timeDiff;
  255.         if (shipSpeed > 0)
  256.         {
  257.             shipSpeed = 0;
  258.         }
  259.     }
  260.     if (shipSpeed != 0)
  261.     {
  262.         moveShip(timeDiff);
  263.     }
  264.     moveShots(timeDiff);
  265.     if(shootNow == 1)
  266.     {
  267.         var myShot:Shot = new Shot();
  268.         myShot.x = myShip.x;
  269.         myShot.y = myShip.y;
  270.         myShot.rotation = myShip.rotation;
  271.         var shotAngleRadians:Number = (myShot.rotation/180)*(Math.PI);
  272.         myShot.dx = Math.cos(shotAngleRadians)*(shipSpeed+maxSpeed*2);
  273.         myShot.dy = Math.sin(shotAngleRadians)*(shipSpeed+maxSpeed*2);
  274.         allShots.push(myShot);
  275.         addChild(myShot);
  276.         moveShots(timeDiff);
  277.     }
  278. }
  279.  
  280. function moveShip(timeDiff:Number)
  281. {
  282.    
  283.     var shipPos:Point = new Point(myShip.x,myShip.y);
  284.     var shipAngle:Number = myShip.rotation;
  285.     var shipAngleRadians:Number = (shipAngle/360)*(2.0*Math.PI);
  286.     var shipMove:Number = shipSpeed*timeDiff;
  287.     var dx:Number = Math.cos(shipAngleRadians)*shipMove;
  288.     var dy:Number = Math.sin(shipAngleRadians)*shipMove;
  289.     newX = shipPos.x+dx;
  290.     newY = shipPos.y+dy;
  291.     if(newX>=myShip.width/2 && newX<=(800-myShip.width/2))
  292.     {
  293.         myShip.x = newX;
  294.     }
  295.     if(newY>=myShip.height/2 && newY<=(600-myShip.height/2))
  296.     {
  297.         myShip.y = newY;
  298.     }
  299. }
  300. function moveShots(timeDiff:Number)
  301.     {
  302.         for(var i:int;i<allShots.length; i++)
  303.         {
  304.             var shotPos:Point = new Point(allShots[i].x, allShots[i].y);
  305.             if(allShots[i].x>0 && allShots[i].x<800 && allShots[i].y>0 && allShots[i].y<600)
  306.             {
  307.                 allShots[i].x += allShots[i].dx*timeDiff;
  308.                 allShots[i].y += allShots[i].dy*timeDiff;
  309.             }
  310.             else
  311.             {
  312.                 removeChild(allShots[i]);
  313.                 allShots.splice(i,1);
  314.             }
  315.         }
  316.     }
  317.  
  318.  
  319. initGoodies();
  320. initBadies();
Add Comment
Please, Sign In to add comment