利用闲暇时间设计了一个简单的射击游戏,玩法很简单,鼠标点击运动的气球,点中一个气球得5分,当生成完20个气球后游戏结束。
游戏设计思路如下:
首先运用TimerEvent类设置一个计时器,设置一定的时间内生成一定数量的气球。定义一容器"conter"装载生成的气球。运用ENTERFRAME定义气球匀速运动。设置鼠标隐藏,定义一个MOUSE_MOVE设置一个替代鼠标,再定义一个鼠标点击事件,当鼠标点中气球时候时触发气球行为。
程序源代码如下:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.events.Event;
import flash.ui.Mouse;
import flash.text.*;
import gs.TweenMax;
import fl.motion.easing.*;
public class shootplay extends MovieClip {
private var smc:aim;
private var mb:qiqiou;
private var q:int=7;//运动速度
private var t:Timer;
private var conter:MovieClip;
private var chishu:int;
private var s:int;//射中个数
private var score:int;//获得分数
public function shootplay() {
begin();
}
private function begin() {
Mouse.hide();
smc = new aim();
addChild(smc);
gomc.visible = false;
again_btn.visible = false;
s = 0;
score = 0;
chishu=0;
stage.addEventListener(MouseEvent.MOUSE_MOVE,mcMove);
t=new Timer(800,20);
t.addEventListener(TimerEvent.TIMER,fuzhi);
t.start();
}
private function mcMove(e:MouseEvent) {
smc.x=this.mouseX;
smc.y=this.mouseY;
}
private function fuzhi(e:TimerEvent) {
chishu+=1;
conter = new MovieClip();
addChild(conter);
setChildIndex(conter,0);
mb = new qiqiou();
conter.addChild(mb);
mb.x=Math.random()*600+20;
mb.y=270;
mb.addEventListener(MouseEvent.CLICK,shoot);
mb.addEventListener(Event.ENTER_FRAME,fly);
trace(chishu);
if (chishu==20) {
gomc.visible = true;
again_btn.visible = true;
gomc.scaleX=0.1;
gomc.scaleY=0.1;
again_btn.alpha=0;
TweenMax.to(gomc, 1, {scaleX:1, scaleY:1,ease:Bounce.easeOut});
TweenMax.to(again_btn, 1, {alpha:1,ease:Bounce.easeOut});
Mouse.show();
removeChild(smc);
again_btn.addEventListener(MouseEvent.MOUSE_DOWN,ckdown);
again_btn.addEventListener(MouseEvent.MOUSE_OVER,ckover);
again_btn.addEventListener(MouseEvent.MOUSE_OUT,ckout);
}
}
private function shoot(e:MouseEvent) {
var _mc:MovieClip=e.currentTarget as MovieClip;
_mc.gotoAndPlay(2);
s+=1;
score+=5;
tongji.fengshu.text = score;
tongji.shezhong.text = s;
}
private function fly(e:Event) {
var _mc:MovieClip=e.currentTarget as MovieClip;
_mc.y-=q;
if (_mc.y<=-21) {
_mc.visible = false;
}
}
private function ckdown(e:MouseEvent) {
begin();
}
private function ckover(e:MouseEvent) {
e.currentTarget.gotoAndStop(2);
}
private function ckout(e:MouseEvent) {
e.currentTarget.gotoAndStop(1);
}
}
}
