我用as2写过了http://blog.csdn.net/adreamstar/archive/2008/02/25/2119524.aspx。
但今天用as3写还是BUG,等以后能解决了再改把。哎,伤心~!!!
源文件下载: http://download.csdn.net/source/383534
下面是代码:
package adreamstar{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.utils.getDefinitionByName;
import flash.display.DisplayObject;
public class Control extends Sprite {
private var updir:Boolean;
private var downdir:Boolean;
private var leftdir:Boolean;
private var rightdir:Boolean;
private var isSend:Boolean;
private var _speed:uint;
private var bulletContainer:Sprite;
private var index:Number=0;
private var enemyContainer:Sprite;
private var enemyBulletContainer:Sprite;
private var isSendEnemyBullet:Boolean=true;
private var count:Number=0;
public function Control():void {
_speed=10;
var plane:Plane=new Plane();
plane.x=300;
plane.y=380;
addChild(plane).name="planes";
bulletContainer=new Sprite();
addChild(bulletContainer);
enemyContainer=new Sprite();
addChild(enemyContainer);
enemyBulletContainer=new Sprite();
addChild(enemyBulletContainer);
createEnemy();
stage.addEventListener(KeyboardEvent.KEY_DOWN,key_down);
stage.addEventListener(KeyboardEvent.KEY_UP,key_up);
addEventListener(Event.ENTER_FRAME,planeEnterFrame);
}
private function createEnemy():void{
for (var i:Number=0; i<4; i++) {
var enemy:Enemy=new Enemy();
enemy.x=Math.random()*400+20;
enemy.y=Math.random()*80+20;
enemyContainer.addChild(enemy);
var enemyBullet:Bullet=new Bullet();
enemyBulletContainer.addChild(enemyBullet);
enemyBullet.x=enemy.x;
enemyBullet.y=enemy.y;
}
}
private function key_down(event:KeyboardEvent):void {
if (event.keyCode==38) {
updir=true;
}
if (event.keyCode==40) {
downdir=true;
}
if (event.keyCode==37) {
leftdir=true;
}
if (event.keyCode==39) {
rightdir=true;
}
if (event.keyCode==32) {
isSend=true;
index++;
var bullet:Bullet=new Bullet();
bulletContainer.addChild(bullet).name="bullet"+index;
bullet.x=getChildByName("planes").x;
bullet.y=getChildByName("planes").y;
}
}
private function key_up(event:KeyboardEvent):void {
if (event.keyCode==38) {
updir=false;
}
if (event.keyCode==40) {
downdir=false;
}
if (event.keyCode==37) {
leftdir=false;
}
if (event.keyCode==39) {
rightdir=false;
}
}
public function planeEnterFrame(event:Event):void {
for(var i:Number=0;i<enemyContainer.numChildren;i++){
enemyContainer.getChildAt(i).x+=10-Math.random()*20;
enemyBulletContainer.getChildAt(i).y+=10;
if(getChildByName("planes").hitTestObject(enemyBulletContainer.getChildAt(i))){
removeChild(getChildByName("planes"));
}
if(enemyBulletContainer.getChildAt(i).y>400){
enemyBulletContainer.removeChildAt(i);
if(enemyContainer.getChildAt(i)!=null){
var enemyBullet:Bullet=new Bullet();
enemyBulletContainer.addChild(enemyBullet);
enemyBullet.x=enemyContainer.getChildAt(i).x;
enemyBullet.y=enemyContainer.getChildAt(i).y;
}
}
}
if(count==4){
for(var j:Number=0;j<enemyBulletContainer.numChildren;j++){
enemyBulletContainer.removeChildAt(j);
}
createEnemy();
count=0;
}
if (updir) {
getChildByName("planes").y-=_speed;
}
if (rightdir) {
getChildByName("planes").x+=_speed;
}
if (downdir) {
getChildByName("planes").y+=_speed;
}
if (leftdir) {
getChildByName("planes").x-=_speed;
}
if (isSend) {
for (var n:Number=0; n<bulletContainer.numChildren; n++) {
bulletContainer.getChildAt(n).y-=15;
for (var m:Number=0; m<enemyContainer.numChildren; m++) {
if (bulletContainer.getChildAt(n).hitTestObject(enemyContainer.getChildAt(m))) {
bulletContainer.removeChildAt(n);
enemyContainer.removeChildAt(m);
count++;
}
}
}
}
}
}
}