简单小游戏制作:射击类游戏(二)

发布时间:2013-3-27浏览:

核心提示:简单小游戏制作:射击类游戏(二)

第二部分开始类的编写:

1、首先打开Flash CS4 (CS3、CS4或CS5没什么大的区别)新建项目,按确定。打开项目面板。图:新建项目

新建项目.png

2、点击右边的第一个三角按钮(项目),在下拉菜单中选"新建项目"。在弹出新建项目面板项目名称:填入项目名称。在根文件夹:项中点击右边的浏览按钮,浏览到准备存放项目的目录,按确定。在ActionScript版本:选ActionScript 3.0 。单击"创建项目"按钮,这样项目就建好了。图:项目面板

项目面板.png

3、把AirRaid.fla文件复制到项目文件夹下(省去制作界面)。点击下面的"创建类"图标,在弹出的创建类面板类:的文本框中填入文件名:AirRaid 单击创建类按钮。这样类就创建好了并为我们写好了类的框架,我们可以在框架中添加我们的代码了。图:创建类

创建类.png

图:填入类名

填入类名.png

图:类的框架

类的框架.png

4、编写AirRaid.as类

AirRaid.as类:文档类  AirRaid类是游戏的控制器,绑定AirRaid.fla 。负责生成界面上的飞机、火炮、子弹。显示得分,子弹的剩余数量。注册了键盘按下,释放侦听器,接收用户的键盘操作。注册了进入帧事件侦听器,进行碰撞检测。定义一个飞机计时器,随机生成飞机。检测剩余子弹的数目,当子弹数为零时也就是主角死亡了,移除界面上的飞机,火炮,跳转到重玩按钮。

package { 

        import flash.display.MovieClip;; 

        import flash.display.SimpleButton; 

        import flash.events.KeyboardEvent; 

        import flash.utils.Timer; 

        import flash.events.TimerEvent; 

        import flash.text.TextField; 

        import flash.events.Event; 

         

        public class AirRaid extends MovieClip { 

                private var aagun:AAGun;//火炮 

                private var airplanes:Array;//飞机数组 

                private var bullets:Array;//子弹数组 

                public var leftArrow, rightArrow:Boolean; 

                private var nextPlane:Timer;//不定时生成飞机的计时器 

                private var shotsLeft:int;//得分文本 

                private var shotsHit:int;//子弹数文本 

                 

                public function startAirRaid() { 

                        // 初始化得分数和子弹数 

                        shotsLeft = 20; 

                        shotsHit = 0; 

                        showGameScore(); 

                         

                        // 生成炮加入到舞台上 

                        aagun = new AAGun(); 

                        addChild(aagun); 

                         

                        // 生成飞机、子弹数组 

                        airplanes = new Array(); 

                        bullets = new Array(); 

                         

                        // 键盘按下、释放事件侦听器 

                        stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownFunction); 

                        stage.addEventListener(KeyboardEvent.KEY_UP,keyUpFunction); 

                         

                        // 进入帧事件侦听器,检测子弹击中飞机的碰撞检测。 

                        addEventListener(Event.ENTER_FRAME,checkForHits); 



                        // 生成下一架飞机 

                        setNextPlane(); 

                } 

                //不定时生成飞机 

                public function setNextPlane() { 

                        //1000毫秒至2000毫秒之间生成一架飞机 

                        nextPlane = new Timer(1000 + Math.random() * 1000,1); 

                        // 

                        nextPlane.addEventListener(TimerEvent.TIMER_COMPLETE,newPlane); 

                        nextPlane.start(); 

                } 

                 

                public function newPlane(event:TimerEvent) { 

                        // 随机的边、速度和高度 

                        if (Math.random() > .5) { 

                                var side:String = "left"; 

                        } else { 

                                side = "right"; 

                        } 

                        var altitude:Number = Math.random()*50+20; 

                        var speed:Number = Math.random()*150+150; 

                         

                        // 生成飞机 

                        var p:Airplane = new Airplane(side,speed,altitude); 

                        addChild(p); 

                        airplanes.push(p); 

                         

                        // set time for next plane 

                        setNextPlane(); 

                } 

                 

                // 碰撞检测 

                public function checkForHits(event:Event) { 

                        for(var bulletNum:int=bullets.length-1;bulletNum>=0;bulletNum--){  

                                for (var airplaneNum:int=airplanes.length-1;airplaneNum>=0;airplaneNum--) { 

                                        if (bullets[bulletNum].hitTestObject(airplanes[airplaneNum])) { 

                                                airplanes[airplaneNum].planeHit(); 

                                                bullets[bulletNum].deleteBullet(); 

                                                shotsHit++; 

                                                showGameScore(); 

                                                break; 

                                        } 

                                } 

                        } 

                         

                        if ((shotsLeft == 0) && (bullets.length == 0)) { 

                                endGame(); 

                        } 

                } 

                 

                // 按下键盘 

                public function keyDownFunction(event:KeyboardEvent) { 

                        if (event.keyCode == 37) { 

                                leftArrow = true; 

                        } else if (event.keyCode == 39) { 

                                rightArrow = true; 

                        } else if (event.keyCode == 32) { 

                                fireBullet(); 

                        } 

                } 

                 

                // 释放键盘 

                public function keyUpFunction(event:KeyboardEvent) { 

                        if (event.keyCode == 37) { 

                                leftArrow = false; 

                        } else if (event.keyCode == 39) { 

                                rightArrow = false; 

                        } 

                } 



                // 生成新的子弹 

                public function fireBullet() { 

                        if (shotsLeft <= 0) return; 

                        var b:Bullet = new Bullet(aagun.x,aagun.y,-300); 

                        addChild(b); 

                        bullets.push(b); 

                        shotsLeft--; 

                        showGameScore(); 

                } 

                 

                public function showGameScore() { 

                        showScore.text = String("得分: "+shotsHit); 

                        showShots.text = String("剩余子弹: "+shotsLeft); 

                } 

                 

                // 从数组获取飞机 

                public function removePlane(plane:Airplane) { 

                        for(var i in airplanes) { 

                                if (airplanes[i] == plane) { 

                                        airplanes.splice(i,1); 

                                        break; 

                                } 

                        } 

                } 

                 

                // 获取数组的一个子弹 

                public function removeBullet(bullet:Bullet) { 

                        for(var i in bullets) { 

                                if (bullets[i] == bullet) { 

                                        bullets.splice(i,1); 

                                        break; 

                                } 

                        } 

                } 

                 

                // 游戏结束,移除界面上的东西。 

                public function endGame() { 

                        // 移除飞机 

                        for(var i:int=airplanes.length-1;i>=0;i--) { 

                                airplanes[i].deletePlane(); 

                        } 

                        airplanes = null; 

                         

                        aagun.deleteGun(); 

                        aagun = null; 

                        // 移除侦听器 

                        stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownFunction); 

                        stage.removeEventListener(KeyboardEvent.KEY_UP,keyUpFunction); 

                        removeEventListener(Event.ENTER_FRAME,checkForHits); 

                         

                        nextPlane.stop(); 

                        nextPlane = null; 

                         

                        gotoAndStop("gameover"); 

                } 



        } 

}


5、编写AAGun.as类
创建类的方法同第3步。首先选中库中的AAGun元件,打开项目面板创建类。在"将类邦定到库元件"前面打勾,点下"使用所选库元件"的单选按钮。图:邦定库元件

邦定库元件.png

AAGun类:邦定库中的炮元件,向左移动向右移动,检测边界,自动消毁。


package {

        import flash.display.*;

        import flash.events.*;

        import flash.utils.getTimer;

        

        public class AAGun extends MovieClip {

                static const speed:Number = 150.0;

                private var lastTime:int; // 控制移动的Timer

                

                public function AAGun() {

                        // 枪的初始位置

                        this.x = 275;

                        this.y = 340;

                        

                        //运动

                        addEventListener(Event.ENTER_FRAME,moveGun);

                }



                public function moveGun(event:Event) {

                        // 得到时差

                        var timePassed:int = getTimer() - lastTime;

                        lastTime += timePassed;

                        

                        // 现在的位置

                        var newx = this.x;

                        

                        // 移动到左边

                        if (MovieClip(parent).leftArrow) {

                                newx -= speed*timePassed / 1000;

                        }

                        

                        // 移动到右边

                        if (MovieClip(parent).rightArrow) {

                                newx += speed*timePassed / 1000;

                        }

                        

                        // 检测边界

                        if (newx < 10) newx = 10;

                        if (newx > 540) newx = 540;

                        

                        // 更新位置

                        this.x = newx;

                }

                

                // 移除屏幕上的炮和事件

                public function deleteGun() {

                        parent.removeChild(this);

                        removeEventListener(Event.ENTER_FRAME,moveGun);

                }

        }

}


新建文件Airplane.as
Airplane类:邦定库中的飞机元件。随机产生飞机,随机的速度,产生5种飞机样式,检测是否飞出边界,检测被子弹击中。自动毁灭和爆炸效果


package {

        import flash.display.MovieClip;

        import flash.events.TimerEvent;

        import flash.utils.Timer;

        import flash.utils.getTimer;

        import flash.events.Event;

        

        public class Airplane extends MovieClip {

                private var dx:Number; // 飞机的速度、方向

                private var lastTime:int; // 控制移动的Timer

                //左右移动

                public function Airplane(side:String, speed:Number, altitude:Number) {

                        if (side == "left") {

                                this.x = -50; // start to the left

                                dx = speed; // fly left to right

                                this.scaleX = -1; // reverse

                        } else if (side == "right") {

                                this.x = 600; // start to the right

                                dx = -speed; // fly right to left

                                this.scaleX = 1; // not reverse

                        }

                        this.y = altitude; // vertical position

                        

                        this.gotoAndStop(Math.floor(Math.random()*5+1));

                        // set up animation

                        addEventListener(Event.ENTER_FRAME,movePlane);

                        lastTime = getTimer();

                }

                //检测边界

                public function movePlane(event:Event) {

                        // get time passed

                        var timePassed:int = getTimer()-lastTime;

                        lastTime += timePassed;

                        

                        // move plane

                        this.x += dx*timePassed/1000;

                        

                        // check to see if off screen

                        if ((dx < 0) && (x < -50)) {

                                deletePlane();

                        } else if ((dx > 0) && (x > 600)) {

                                deletePlane();

                        }

                        

                }

                

                // 检测碰撞,子弹击中飞机跳转到帧标签"explode"演示爆炸动画。

                public function planeHit() {

                        removeEventListener(Event.ENTER_FRAME,movePlane);

                        MovieClip(parent).removePlane(this);

                        gotoAndPlay("explode");

                }

                

                // 移除舞台上的飞机和事件

                public function deletePlane() {

                        removeEventListener(Event.ENTER_FRAME,movePlane);

                        MovieClip(parent).removePlane(this);

                        parent.removeChild(this);

                }

      &,nbsp;         

    &n,bsp;,;   }

}


新建文件Bullet.as
Bullet类:邦定库中的子弹元件。控制子弹的飞行方向,检测子弹是否飞出屏幕。


package {

        import flash.display.MovieClip;

        import flash.events.Event;

        import flash.utils.getTimer;

        import flash.utils.Timer;

        import flash.events.TimerEvent;

        

        public class Bullet extends MovieClip {

                private var dy:Number; // 子弹的速度、方向。

                private var lastTime:int;

                

                public function Bullet(x,y:Number, speed: Number) {

                        // 初始位置

                        this.x = x;

                        this.y = y;

                        // 得到速度

                        dy = speed;

                        // 动画

                        lastTime = getTimer();

                        addEventListener(Event.ENTER_FRAME,moveBullet);

                }

                

                public function moveBullet(event:Event) {

                        // 得到时差

                        var timePassed:int = getTimer()-lastTime;

                        lastTime += timePassed;

                        

                        // 子弹运动

                        this.y += dy*timePassed/1000;

                        

                        // 子弹越过屏幕的顶端

                        if (this.y < 0) {

                                deleteBullet();

                        }

                        

                }



                // 移除舞台的子弹和事件

                public function deleteBullet() {

                        MovieClip(parent).removeBullet(this);

                        parent.removeChild(this);

                        removeEventListener(Event.ENTER_FRAME,moveBullet);

                }



        }

}

所有的类文件完成保存后,回到AirRaid.fla场景,按Ctrl+Enter组合键测试,看看你的成果吧!

附件下载:
AirRaid.fla.rar      项目png.rar

 

前一篇:定做程序设计
后一篇:简单小游戏制作:射击类游戏(一)
分享到: