很久之前自己写的太空游戏flash,游戏效果还不错 哈哈,爽!,写得不好,呵呵:
部分代码如下:
/**
*Author:luodongfu
**/
import flash.events.Event;
import flash.events.TimerEvent;
import flash.net.URLRequest;
import flash.media.Sound;
import flash.events.KeyboardEvent;
import flash.ui.*;
import fl.transitions.Tween;
import fl.transitions.easing.*;
//敌人普通飞机的保存数组
var places:Array=new Array();
//敌人降落伞工兵数组
var sans:Array=new Array();
//我方发射的炮弹数组
var paos:Array=new Array();
//敌方发射的炮弹
var djPaos:Array=new Array();
//敌方的智能轰炸机
var djPlaces:Array=new Array();
var da:Number=10;
//我方的坦克
var tange:TanGeMC=new TanGeMC();
tange.x=300;
tange.y=350;
addChild(tange);
//按键左右建向左右移动我方坦克
stage.addEventListener(KeyboardEvent.KEY_DOWN,startMove);
//按空格建发射坦克炮弹
stage.addEventListener(KeyboardEvent.KEY_UP,faPaoFun);
//每贞侦听函数
stage.addEventListener(Event.ENTER_FRAME,onEnterFrameAction);
//炮弹爆炸声音
var paozhaRequest:URLRequest=new URLRequest("Fx12693_cannon4.mp3");
var paozhaSound:Sound=new Sound(paozhaRequest);
var djSPao:Number=0;
//降落伞爆炸声音
var sanBombSndRequest:URLRequest=new URLRequest("Fx13423_bomb1.mp3");
var sanBombSound:Sound=new Sound(sanBombSndRequest);
//坦克发射炮弹时声音
var kaiPaoRequest:URLRequest=new URLRequest("Fx12077_tankfire.mp3");
var kaiPaoSound:Sound=new Sound(sanBombSndRequest);
//定时产生几个一号敌机
var gPlaceTimer:Timer=new Timer(8000);
gPlaceTimer.addEventListener(TimerEvent.TIMER,createPlace);
gPlaceTimer.start();
//定时产生几个二号敌机
var rPlaceTimer:Timer=new Timer(10000);
rPlaceTimer.addEventListener(TimerEvent.TIMER,createPlace2);
rPlaceTimer.start();
//产生敌人智能轰炸机
var djPlace_mc:DJPlaceMC=new DJPlaceMC();
djPlace_mc.x=250+Math.random()*80;
djPlace_mc.y=100+Math.random()*20;
addChild(djPlace_mc);
djPlaces.push(djPlace_mc);
function removeSanListener(k:uint,san:LuoSanMC):void
{
san.removeSanListener();
removeChild(san);
sans.splice(k,1);
san=null;
trace("del san outsize of stage ok and the san size is "+(sans.length-1));
}
开始游戏,生成飞机坦克
function onEnterFrameAction(e:Event):void
{
for (var k:uint=0; k<sans.length; k++)
{
var isDelSan:Boolean=false;
var tempSan=sans[k];
for (var a:uint=0; a<paos.length; a++)
{
var tempPao=paos[a];
//如果炮弹击中降落伞,则降落伞移除并爆炸。报摊移除
if (tempPao.hitTestObject(tempSan))
{
tempSan.gotoAndPlay(2);
paozhaSound.play();
sans.splice(k,1);
tempSan=null;
isDelSan=true;
removeChild(tempPao);
paos.splice(a,1);
tempPao=null;
break;
}
}
if (isDelSan)
{
continue;
}
if (!isDelSan &&tempSan.hitTestObject(diMian_mc))
{
isDelSan=true;
sanBombSound.play();
removeSanListener(k,tempSan);
}
if (!isDelSan &&(tempSan.y>400 ||tempSan.x<0))
{
removeSanListener(k,tempSan);
}
}
for (var i:uint=0; i<places.length; i++)
{
var tempPlace=places;
tempPlace.x-=10;
var isDel:Boolean=false;
for (var n:uint=0; n<paos.length; n++)
{
var pao=paos[n];
if (pao.hitTestObject(tempPlace))
{
tempPlace.gotoAndPlay(3);
paozhaSound.play();
//removeChild(tempPlace);
places.splice(i,1);
tempPlace.stopSound();
tempPlace=null;
isDel=true;
removeChild(pao);
paos.splice(n,1);
pao=null;
var myRan:Number=Math.random()*10;
if(myRan>5)
{
createPlace(null);
}
else
{
createPlace2(null);
}
break;
}
}
if (!isDel&&tempPlace.x<0 )
{
removeChild(tempPlace);
places.splice(i,1);
tempPlace.stopSound();
tempPlace=null;
isDel=true;
continue;
}
var ran:Number=Math.random()*100;
if (ran>94 &&!isDel)
{
var san:LuoSanMC=new LuoSanMC(tempPlace.x+20,tempPlace.y+20);
this.addChild(san);
sans.push(san);
}
}
for (var m:uint=0; m<paos.length; m++)
{
var myPao=paos[m];
var delMyPao:Boolean=false;
for ( n=0; n<djPlaces.length; n++)
{
var djPlace=djPlaces[n];
if(djPlace.hitTestObject(myPao))
{
djSPao++;
if(djSPao>25)
{
djPlace.gotoAndPlay(2);
paozhaSound.play();
djSPao=0;
djPlaces.splice(n,1);
}
delMyPao=true;
removeChild(myPao);
paos.splice(m,1);
}
}
if(!delMyPao)
{
if (myPao.y<-5)
{
removeChild(myPao);
paos.splice(m,1);
continue;
}
myPao.y-=7;
}
}
var ran2:Number=Math.random()*100;
if (ran2>80 &&djPlaces.length>0)
{
newDPlacePao();
}
var ran3:Number=Math.random()*10;
if(ran3>=7&& djPlaces.length==0)
{
newDPlaceMC();
}
}
.................
....................................................................................
.......................................................