发新话题
打印

[分享] 飞机游戏制作(AS2和AS3)

飞机游戏制作(AS2和AS3)

前一半是AS3,后一半是AS2。大家觉得有用的话帮我顶啊,谢谢~!!!
AS3:Bullet.as,BulletMove.as,Control.as,Enemy.as,Plane.as,AS3就写了一点,没写完,感觉写不下去了,希望大虾提点建议啊。
BulletMove.as
复制内容到剪贴板
代码:
package{
import flash.display.Sprite;
import flash.events.Event;
public class BulletMove extends Sprite{
  private var _speed:int=10;
  public function BulletMove(){
   var bullet:Bullet=new Bullet(5,0xffff00ff);
   addChild(bullet);
   addEventListener(Event.ENTER_FRAME,bulletMove);
  }
  public function bulletMove(event:Event):void{
   event.target.y-=_speed;
   
  }
}
}
Control.as:
复制内容到剪贴板
代码:
package {
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.display.DisplayObject;
public class Control extends Sprite {
  private var _speed:int=5;
  private var _planeBulletSprite:Sprite;
  public function Control() {
       var plane:Plane=new Plane(30,20,0xff00ffff);
   addChild(plane).name="planes";
   plane.x=250;
   plane.y=350;
   var enemy:Enemy=new Enemy(20,20,0xff0000ff);
   enemy.x=Math.random()*400+30;
   enemy.y=Math.random()*50+20;
   addChild(enemy).name="enemys";
   _planeBulletSprite=new Sprite();
   addChild(_planeBulletSprite);
   addEventListener(Event.ENTER_FRAME,planeEnterFrame);
   
  }
  public function planeEnterFrame(event:Event):void{
   stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDownEvent);
  }
  public function onKeyDownEvent(event:KeyboardEvent):void {
   if(event.keyCode==Keyboard.UP){
    getChildByName("planes").y-=_speed;
    }
   if (event.keyCode==Keyboard.DOWN) {
    getChildByName("planes").y+=_speed;
    }
   if(event.keyCode==Keyboard.LEFT){
    getChildByName("planes").x-=_speed;
    }
   if(event.keyCode==Keyboard.RIGHT){
    getChildByName("planes").x+=_speed;
    }
   if(event.keyCode==Keyboard.SPACE){
    bulletSend();
   }
   
  }
  public function bulletSend():void{
   var bullet:BulletMove=new BulletMove();
   _planeBulletSprite.addChild(bullet).name="bullets";
   bullet.x=getChildAt(0).x+45;
   bullet.y=getChildAt(0).y-15;
   if(getChildByName("enemys").hitTestObject(_planeBulletSprite)){
        removeChild(DisplayObject(getChildByName("enemys")));
        var enemy:Enemy=new Enemy(20,20,0xff0000ff);
        enemy.x=Math.random()*400+30;
        enemy.y=Math.random()*50+20;
        addChild(enemy).name="enemys";
   }
  }
  
}
}
感觉AS3写的碰撞检测很延迟啊,过一段时间后才能删去啊。。。。
下面是AS2:
as2压缩文件里分了5步来写飞机游戏,代码里有注释很详细的。
复制内容到剪贴板
代码:
/*作者:梦自在*/
stop();
/*声音效果*/
var bgSound:Sound = new Sound();
var enemySound:Sound = new Sound();
bgSound.attachSound("bg_wav");
enemySound.attachSound("bullet_wav");
bgSound.start(0, 30000);
score_txt.text = 0;
var killEnemy_num:Number = 0;
var enemy_speed:Number;
/*定义运动速度*/
var speed:Number = 15;
/*检测杀死敌人的数量*/
var enemy_num:Number = 0;
/*敌人子弹是不是已经发出*/
var isEnemyBulletSent:Boolean;
_root.createEmptyMovieClip("bullet_container", 3);
function createEnemy_func() {
/*敌人的图层*/
isCreateEnemy = true;
_root.createEmptyMovieClip("enemy_container", 1);
for (var i:Number = 0; i<=4; i++) {
  /*从库中把enemy复制出来*/
  enemy_container.attachMovie("enemy", "newenemy"+i, -100+i);
  /*位子*/
  enemy_container["newenemy"+i]._x = 30+Math.random()*500;
  enemy_container["newenemy"+i]._y = Math.random()*100;
  enemy_speed = Math.random()*20-10;
  createEnemyBullet_func();
  enemy_container["newenemy"+i].v = enemy_speed;
  enemy_container["newenemy"+i].onEnterFrame = function() {
   this._x += this.v;
   this._x>530 || this._x<30 ? this.v=-this.v : 梦自在;
  };
}
}
function createEnemyBullet_func() {
/*子弹的图层*/
_root.createEmptyMovieClip("enemyBullet_container", 2);
for (var i:Number = 0; i<=4; i++) {
  /*当飞机被击毁后就不能再发子弹了*/
  if (enemy_container["newenemy"+i].getDepth()<0) {
   enemyBullet_container.attachMovie("bullet", "enemyBullet"+i, i+10);
   enemyBullet_container["enemyBullet"+i]._x = enemy_container["newenemy"+i]._x;
   enemyBullet_container["enemyBullet"+i]._y = enemy_container["newenemy"+i]._y;
   enemyBullet_container["enemyBullet"+i].onEnterFrame = function() {
    this._y += Math.random()*6+4;
    /*子弹碰到飞机后就结束游戏*/
    if (this.hitTest(plane_mc._x, plane_mc._y)) {
     enemyBullet_container.removeMovieClip();
     enemy_container.removeMovieClip();
     plan_mc.removeMovieClip();
     gotoAndStop(2);
    }
    if (this._y>440) {
     isEnemyBulletSent = false;
     this.removeMovieClip();
    }
   };
  }
}
}
onEnterFrame = function () {
/*这样写一次只能发射一个子弹*/
if (!isEnemyBulletSent) {
  isEnemyBulletSent = true;
  createEnemyBullet_func();
}
};
/*控制方向的键,对这指令不懂的话按F1查看帮助*/
plane_mc.onEnterFrame = function() {
/*方向键按上*/
if (Key.isDown(Key.UP)) {
  this._y -= speed;
  this._y<10 ? this._y=10 : 梦自在;
}
if (Key.isDown(Key.DOWN)) {
  this._y += speed;
  this._y>380 ? this._y=380 : 梦自在;
}
if (Key.isDown(Key.LEFT)) {
  this._x -= speed;
  this._x<10 ? this._x=10 : 梦自在;
}
if (Key.isDown(Key.RIGHT)) {
  this._x += speed;
  this._x>530 ? this._x=530 : 梦自在;
}
/*如果你按空格键并且子弹没发射出去的话*/
if (Key.isDown(Key.SPACE)) {
  /*获取当前的最底深度*/
  enemySound.start();
  var depth:Number = bullet_container.getNextHighestDepth();
  bullet_container.attachMovie("bullet", "newbullet"+depth, depth);
  bullet_container["newbullet"+depth]._x = plane_mc._x;
  bullet_container["newbullet"+depth]._y = plane_mc._y;
  bullet_container["newbullet"+depth].onEnterFrame = function() {
   this._y -= speed;
   /*对于舞台上面所有的敌人,如果碰到子弹就子弹和敌人一起删去*/
   for (var i in enemy_container) {
    if (enemy_container[i].hitTest(this._x, this._y)) {
     /*深度为正才能用removeMovieClip删去*/
     this.removeMovieClip();
     enemy_container[i].swapDepths(1);
     enemy_container[i].removeMovieClip();
     enemy_num += 1;
     score_txt.text = enemy_num;
     killEnemy_num += 1;
    }
   }
   if (killEnemy_num == 5) {
    createEnemy_func();
    killEnemy_num = 0;
   }
  };
}
};
createEnemy_func();

附件

plane5.swf (10.45 KB)

2008-1-24 11:33, 下载次数: 177

as2最终效果

as2.rar (158.7 KB)

2008-1-24 11:33, 下载次数: 216

as3.rar (8.08 KB)

2008-1-24 11:33, 下载次数: 97

TOP

我来项!!

TOP

我也来顶你...历害

TOP

谢谢啊
AS3写了一点,子弹碰到敌人后敌人过一段时间才能消失,象AS2一碰到就消失了,不知道是什么原因。汗~!!!

TOP

顶!支持原创~
我擦一擦嘴巴,不留下一颗饭粒~

TOP

引用:
原帖由 梦自在 于 2008-1-24 11:43 发表
谢谢啊
AS3写了一点,子弹碰到敌人后敌人过一段时间才能消失,象AS2一碰到就消失了,不知道是什么原因。汗~!!!
AS3我怎么打不到,一按空格飞机就跑了

TOP

TOP

我在帮助里找跟有关的hitTest,找了半天才找到hitTestObject,hitTestPoint,hitTest,发现只有hitTestObject有用,
if(getChildByName("enemys").hitTestObject(_planeBulletSprite)){
除了这样写不报错 ,换成别的都报错,我伤心,感觉这方面的资料少了。子弹碰到了敌人周围的一点距离,敌人都还要过一段时间才消失的,。所以我说写不下去了啊。还请各位大虾矫正。。。

TOP

子弹设计的不好,按住不放没有动感,像连珠
寻觅终生伴侣!

my blog

TOP

你这个检测是放在bulletSend()里的
而bulletSend()是在键盘监听里的
放在ONENTERFARME里试一试

TOP

ycccc8202:汗,AS2写的大致象个飞机游戏,反正只是提供个思路把。我看别人飞机游戏里面代码都很喜欢藏,我想把这全部写在第一帧应该看的清楚些把。。。。
kingofkofs:放在onEnterFrame没延迟了,我hitTestObject里面的参数换成别的都报错,
hitTestObject帮助里面写的例子都是sprite,shape对象啊,找了半天,才找到能不报错的参数,功能就发生了变化啊,语法好恶心啊~!!!
郁闷

TOP

无敌方法:按空格不放左右来回

TOP

说实在的空战射击游戏做起来费力不讨好。

TOP

谢谢大大啊
找了很久了

TOP

借你的地方问个问题
不好意思,刚刚注册还不能发新帖,到这里问一个问题,有高手路过请指教一下
我用js判断客户机浏览器是否安装了flash插件,安装了就显示flash,否则到另外一个页面。
但是发现一个问题,有的客户机没有安装flash插件,但是可能是因为装了迅雷看看一类的东西,
他的浏览器也能正常显示flash。
这样就会漏掉一些其实能正常显示flash的客户机,让他们到另一个页面去了。
有没有一种方法直接判断客户机浏览器能否播放flash,而不是只是判断是否装了flash插件。

TOP

收藏学习,谢谢!

TOP

发新话题