Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import flash.events.KeyboardEvent;
- import flash.events.Event;
- import flash.geom.Point;
- //Onda/Goda objekt OSV
- var goodieAmount:int =25;
- var allGoodies:Array = new Array();
- var badieAmount:int = 10;
- var allBadies:Array = new Array();
- //Piltangenter OSV
- var leftArrow:Boolean = false;
- var rightArrow:Boolean = false;
- var upArrow:Boolean = false;
- var downArrow:Boolean = false;
- var shootNow:Boolean = false;
- //Skepparvariabler OSV
- var shipSpeed:Number = 0;
- var shipAccel:Number = 0.002;
- var shipDecel:Number = 0.0003;
- var maxSpeed:Number = 0.3;
- var shipRot:Number = 0;
- var turnSpeed:Number = 0.4;
- var newY:int;
- var newX:int;
- var lastTime:int = 0;
- var myShip:shipMC = new shipMC();
- myShip.x = 400;
- myShip.y = 300;
- addChild(myShip);
- var allShots:Array = new Array;
- var point = 0;
- var antalliv = 5;
- score.text = "Score "
- lives.text ="Liv " + antalliv;
- //Lyssnare OSV
- stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
- stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
- this.addEventListener(Event.ENTER_FRAME,appLoop);
- this.addEventListener(Event.ENTER_FRAME,objectLoopGood);
- this.addEventListener(Event.ENTER_FRAME,objectLoopBad);
- //Funktioner
- //Initieringsfunktion goda/onda objekt
- function initGoodies():void {
- for(var i:int=0;i<goodieAmount;i++){
- var myGoodie:Goodie = new Goodie();
- myGoodie.x = 50 + Math.floor(700*Math.random());
- myGoodie.y = 50 + Math.floor(500*Math.random());
- myGoodie.dx = 5 - Math.floor(10*Math.random());
- myGoodie.dy = 5 - Math.floor(10*Math.random());
- allGoodies.push(myGoodie);
- addChild(myGoodie);
- }
- }
- function initBadies():void {
- for(var i:int=0;i<badieAmount;i++){
- var myBadie:Badie = new Badie();
- myBadie.x = 50 + Math.floor(700*Math.random());
- myBadie.y = 50 + Math.floor(500*Math.random());
- myBadie.dx = 5 - Math.floor(10*Math.random());
- myBadie.dy = 5 - Math.floor(10*Math.random());
- allBadies.push(myBadie);
- addChild(myBadie);
- }
- }
- function keyPressedDown(event:KeyboardEvent) {
- if (event.keyCode == 37) {
- leftArrow = true;
- } else if (event.keyCode == 39) {
- rightArrow = true;
- } else if (event.keyCode == 38) {
- upArrow = true;
- } else if (event.keyCode == 40) {
- downArrow = true;
- } else if (event.keyCode == 32) {
- shootNow = true;
- }
- }
- function keyPressedUp(event:KeyboardEvent) {
- if (event.keyCode == 37) {
- leftArrow = false;
- } else if (event.keyCode == 39) {
- rightArrow = false;
- } else if (event.keyCode == 38) {
- upArrow = false;
- } else if (event.keyCode == 40) {
- downArrow = false;
- } else if (event.keyCode == 32) {
- shootNow = false;
- }
- }
- function objectLoopGood(event:Event) {
- for (var j:int= 0;j<allGoodies.length;j++) {
- var newX:Number = allGoodies[j].x + allGoodies[j].dx;
- var newY:Number = allGoodies[j].y + allGoodies[j].dy;
- if(newX<allGoodies[j].width/2) {
- allGoodies[j].x = allGoodies[j].width/2 + (allGoodies[j].width/2 - newX);
- allGoodies[j].dx *= -1;
- }
- else if(newX > (800 - allGoodies[j].width/2)) {
- allGoodies[j].x = (800 - allGoodies[j].width/2) - (newX - (800 - allGoodies[j].width/2));
- allGoodies[j].dx *= -1;
- }
- else {
- allGoodies[j].x = newX;
- }
- //flytta i Y-led
- if(newY<allGoodies[j].height/2) {
- allGoodies[j].y = allGoodies[j].height/2 + (allGoodies[j].height/2 - newY);
- allGoodies[j].dy *= -1;
- }
- else if(newY > (600 - allGoodies[j].height/2)) {
- allGoodies[j].y = (600 - allGoodies[j].height/2) - (newY - (600 - allGoodies[j].height/2));
- allGoodies[j].dy *= -1;
- }
- else {
- allGoodies[j].y = newY;
- }
- //Testa krock med skepp
- var hitDist:Number = (myShip.width/2 + allGoodies[j].width/2);
- var dist:Number = Point.distance(new Point(myShip.x,myShip.y), new Point(allGoodies[j].x,allGoodies[j].y));
- if (dist<hitDist)
- {
- removeChild(allGoodies[j]);
- allGoodies.splice(j,1);
- point+=50;
- score.text = "Score " + point;
- }
- }
- }
- function objectLoopBad(event:Event) {
- for (var j:int= 0;j<allBadies.length;j++) {
- var newX:Number = allBadies[j].x + allBadies[j].dx;
- var newY:Number = allBadies[j].y + allBadies[j].dy;
- if(newX<allBadies[j].width/2) {
- allBadies[j].x = allBadies[j].width/2 + (allBadies[j].width/2 - newX);
- allBadies[j].dx *= -1;
- }
- else if(newX > (800 - allBadies[j].width/2)) {
- allBadies[j].x = (800 - allBadies[j].width/2) - (newX - (800 - allBadies[j].width/2));
- allBadies[j].dx *= -1;
- }
- else {
- allBadies[j].x = newX;
- }
- //flytta i Y-led
- if(newY<allBadies[j].height/2) {
- allBadies[j].y = allBadies[j].height/2 + (allBadies[j].height/2 - newY);
- allBadies[j].dy *= -1;
- }
- else if(newY > (600 - allBadies[j].height/2)) {
- allBadies[j].y = (600 - allBadies[j].height/2) - (newY - (600 - allBadies[j].height/2));
- allBadies[j].dy *= -1;
- }
- else {
- allBadies[j].y = newY;
- }
- //Testa krock med skepp
- var hitDist:Number = (myShip.width/2 + allBadies[j].width/2);
- var dist:Number = Point.distance(new Point(myShip.x,myShip.y), new Point(allBadies[j].x,allBadies[j].y));
- if (dist<hitDist)
- {
- removeChild(allBadies[j]);
- allBadies.splice(j,1);
- point-=25;
- antalliv-=1;
- score.text = "Score " + point;
- lives.text = "Liv " + antalliv;
- if (antalliv<1)
- {
- stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
- stage.removeEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
- this.removeEventListener(Event.ENTER_FRAME,appLoop);
- this.removeEventListener(Event.ENTER_FRAME,objectLoopGood);
- this.removeEventListener(Event.ENTER_FRAME,objectLoopBad);
- gameover1.text = "Game ";
- gameover2.text = "Over!";
- }
- }
- for (var t:int= 0;t<allShots.length;t++)
- {
- var hitbadDist:Number = (allShots[t].width/2 + allBadies[j].width/2);
- var badDist:Number = Point.distance(new Point(allShots[t].x,allShots[t].y), new Point(allBadies[j].x,allBadies[j].y));
- if (badDist<hitbadDist)
- {
- removeChild(allBadies[j]);
- allBadies.splice(j,1);
- removeChild(allShots[t])
- allShots.splice(t,1);
- point+=25;
- score.text = "Score " + point;
- lives.text = "Liv " + antalliv;
- }
- }
- }
- }
- function appLoop(event:Event)
- {
- if (lastTime ==0)
- {
- lastTime = getTimer();
- }
- var timeDiff:int = getTimer()-lastTime;
- lastTime += timeDiff;
- //Höger/Vänsterpilar
- if (leftArrow)
- {
- myShip.rotation -=(shipSpeed+0.1)*turnSpeed*timeDiff;
- }
- if (rightArrow)
- {
- myShip.rotation +=(shipSpeed+0.1)*turnSpeed*timeDiff;
- }
- //Upp/Ned
- if (upArrow)
- {
- shipSpeed += shipAccel*timeDiff;
- if (shipSpeed > maxSpeed)
- {
- shipSpeed = maxSpeed;
- }
- }
- else if (downArrow)
- {
- shipSpeed -= shipAccel*timeDiff;
- if (shipSpeed < maxSpeed)
- {
- shipSpeed = -maxSpeed;
- }
- }
- else if (shipSpeed > 0)
- {
- shipSpeed -= shipDecel*timeDiff;
- if (shipSpeed < 0)
- {
- shipSpeed = 0;
- }
- }
- else if (shipSpeed < 0)
- {
- shipSpeed += shipDecel*timeDiff;
- if (shipSpeed > 0)
- {
- shipSpeed = 0;
- }
- }
- if (shipSpeed != 0)
- {
- moveShip(timeDiff);
- }
- moveShots(timeDiff);
- if(shootNow == 1)
- {
- var myShot:Shot = new Shot();
- myShot.x = myShip.x;
- myShot.y = myShip.y;
- myShot.rotation = myShip.rotation;
- var shotAngleRadians:Number = (myShot.rotation/180)*(Math.PI);
- myShot.dx = Math.cos(shotAngleRadians)*(shipSpeed+maxSpeed*2);
- myShot.dy = Math.sin(shotAngleRadians)*(shipSpeed+maxSpeed*2);
- allShots.push(myShot);
- addChild(myShot);
- moveShots(timeDiff);
- }
- }
- function moveShip(timeDiff:Number)
- {
- var shipPos:Point = new Point(myShip.x,myShip.y);
- var shipAngle:Number = myShip.rotation;
- var shipAngleRadians:Number = (shipAngle/360)*(2.0*Math.PI);
- var shipMove:Number = shipSpeed*timeDiff;
- var dx:Number = Math.cos(shipAngleRadians)*shipMove;
- var dy:Number = Math.sin(shipAngleRadians)*shipMove;
- newX = shipPos.x+dx;
- newY = shipPos.y+dy;
- if(newX>=myShip.width/2 && newX<=(800-myShip.width/2))
- {
- myShip.x = newX;
- }
- if(newY>=myShip.height/2 && newY<=(600-myShip.height/2))
- {
- myShip.y = newY;
- }
- }
- function moveShots(timeDiff:Number)
- {
- for(var i:int;i<allShots.length; i++)
- {
- var shotPos:Point = new Point(allShots[i].x, allShots[i].y);
- if(allShots[i].x>0 && allShots[i].x<800 && allShots[i].y>0 && allShots[i].y<600)
- {
- allShots[i].x += allShots[i].dx*timeDiff;
- allShots[i].y += allShots[i].dy*timeDiff;
- }
- else
- {
- removeChild(allShots[i]);
- allShots.splice(i,1);
- }
- }
- }
- initGoodies();
- initBadies();
Add Comment
Please, Sign In to add comment