邮件订阅
5ucms帮助Flash制作空战游戏(三)
作者: 来源: 日期:2017/2/7 9:55:03 浏览量:21(滞后) 人气:LOADING...(实时) 【

5ucms帮助Flash制作空战游戏(三)
  三、代码控制

  1.回到主场景,把第一个图层改名为“start”,在第一帧中插入关键帧,帧动作为:

  functi>

  6.再增加两个图层,一个命名为 “plane”,一个命名为“enemy”,并分别在第三帧插入关键帧。选中“plane”层的第三帧,把“plane”拖入,剪辑名称为“ship”。选中“enemy”层的第三帧,把“enemy”和“ball”剪辑拖入,剪辑名称分别为“enemy”和“enemylaser”。

  右击“plane”剪辑,在动作面板里写入代码:

  onClipEvent (load) {  //载入时进行初始化
    speed=6;
    sy=_y;
    ang=0;
   } 

  onClipEvent (enterFrame) {
     _y = sy + 4 * Math.cos(ang+=0.092); //用cos函可以是飞机有在空中飘动的效果
     c=_root . score;        //给c 变量赋值,初试状态为“0”
     if(c>=500){         //如果c的值(得分)大于500,则胜利结束游戏
       _root.gotoAndStop(2);
     }                     
     if(_root.life<=0){       //如果生命值小于0,则重新开始游戏或退出
       _root.gotoAndStop(1);

     }

  //当玩家按下上、下、左、右方向键时,飞机可以移动           

     if (Key.isDown(Key.down)and _y<400) {_y += speed; sy+=speed}
     if (Key.isDown(Key.up) and _y>0) {_y -= speed; sy-=speed}
     if (Key.isDown(Key. right) and _X<550) {_x += speed;}
     if (Key.isDown(Key.left) and _X>0) {_x -= speed;}
     if (Key.isDown(Key.space)) {//当按下空格键时,飞机开火,用了一个变量a,使每按一下空格发出一发炮弹,如果不设置这个变量,那么当按下空格键时,炮弹将连续飞出。
       if (!a) {      //如果a为false
         shotCount++;
         depth=(shotCount%100)+100
         _root.attachMovie("laser", "sparo"+shotCount, depth); //attachmovie方法是从库中直接调用复制影片剪辑“fire”,它的标识符为“laser”。
         _root["sparo"+shotCount]._x = this._x+(_width-45)
         _root["sparo"+shotCount]._y = this._y;  //复制出的“fire”剪辑的初试位置。         a = true;

        }
      } else {a = false;}
   } 

  7.右击“enemy”剪辑,在“动作”面板里设置代码

  onClipEvent (load) {  
     sy=random (400)+10; //取随机数
     function reset(){
       ship1.gotoAndStop(1);
       speed=random(10)+2; //敌机的飞行速度是一个随机数
       _y= random(300)+_height; //敌机的初始y坐标取随机数
       _x= 550+_y;  
       _root.firePower=100 ;

     }
     reset()
   }

  onClipEvent (enterFrame) {
    if(this.ship1._currentframe==1){ //敌机剪辑在第一帧
      _y = sy + speed * Math.cos(speed+=0.032); //敌机飞行时有浮动的效果
      if(this.hitTest(_root.ship)){ //如果该剪辑与战斗机相撞
         ship1.gotoAndStop(2); //敌机爆炸
         _root.ship.play(); 
         _root.life-=10; //生命值减10
      }
      if(random(_root.firePower)+1==10){
        Count++;
        depth=(Count%100)+200;   //敌机飞行时发出炮弹
        duplicateMovieClip(_root.enemyLaser, "num"+Count, depth);
        //炮弹的初始坐标就是敌机当前所在位置的坐标
        _root["num"+Count]._x=this._x-40
        _root["num"+Count]._y=this._y 
      }
     }
     _x-=speed
     if(_x<-10) { reset()}

   }



欢迎您访问www.5ucms.org

本站推荐: 5ucms模板下载 5ucms插件下载 仿站联系Q3876307       [复制给好友] [打印] [关闭] [返回] [顶部]
上一篇:5ucms培训,Flash MX 2004视频文件轻松播
下一篇:5ucms帮助DW MX 2004的Flash动画元素 下
本站声明:本网站所载文章等内容,目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如涉及作品内容、版权及其它问题,请在30日内与本网联系(Email:3876307#qq.com),我们将在第一时间删除内容。若原创内容转载请注明出处。
5ucms帮助Flash制作空战游戏(三)的关键词:
评论信息
相关分类
本周热门
本月热门
关于我们 - 版权/免责 申明 - 建站服务 - 网站地图 - 稿件投递 - 联系我们 - 5ucms
Copyright © 2008-2015 www.5ucms.org