易威環(huán)
摘要:該文采用Flash AS3為開發(fā)環(huán)境,首先對(duì)整個(gè)項(xiàng)目策略進(jìn)行簡(jiǎn)述,并配有圖例進(jìn)行說(shuō)明,再在Flash環(huán)境中,設(shè)計(jì)相應(yīng)元件,分各模塊進(jìn)行詳細(xì)設(shè)計(jì),并配有詳細(xì)的說(shuō)明。
關(guān)鍵詞:Flash;AS3;游戲;打字
中圖分類號(hào):TP311文獻(xiàn)識(shí)別碼:A 文章編號(hào):1009-3044(2015)17-0082-03
The Gameof Typewriting to Pass a Test
YI Wei-huan
(Software Institute of Hunan Vocational College of Science and Technology,Changsha 410118,China)
Abstract:In this paper, we use Flash AS3 to setup the development environment. Firstly, the whole project strategy is briefly introduced. and with a legend to explain, Then In the Flash environment,the author designers the corresponding element. Each module detailed design, And is equipped with a detailed description.
Key words: Flash;AS3;game;typewriting
AS3即ActionScript3.0的簡(jiǎn)稱。它是Flash的腳本語(yǔ)言,是一種面向?qū)ο缶幊陶Z(yǔ)言。在Flash中運(yùn)用AS3.0編寫腳本,可以實(shí)現(xiàn)各種復(fù)雜的交互功能和炫酷的視覺(jué)效果,如:純Flash制作的品牌互動(dòng)網(wǎng)站、網(wǎng)站互動(dòng)廣告、產(chǎn)品互動(dòng)展示、Flash小游戲、網(wǎng)絡(luò)游戲等等??梢哉f(shuō)AS3是成為一名Flash開發(fā)高手必備的武器。
1 設(shè)計(jì)思想
屏幕上不斷隨機(jī)出現(xiàn)英文字母,超出屏幕就消失,系統(tǒng)扣除玩定積分,玩家不斷按鍵,擊中相應(yīng)字母則系統(tǒng)加上積分,如果在規(guī)定的時(shí)間內(nèi)能達(dá)到相應(yīng)的分值,則進(jìn)入下一關(guān),否則游戲結(jié)束。如果在規(guī)定的時(shí)間內(nèi),積分如果為0,則游戲也結(jié)束。
2 項(xiàng)目界面
2.1 運(yùn)行界面
3 準(zhǔn)備
1 )打開Flash,新建ActionScript3.0文檔。
2 )在屏幕上添加文本框lbl倒計(jì)時(shí),添加文本框lbl1顯示積分,添加文本框msg顯示相關(guān)信息,添加一按鈕btn,并設(shè)置為不可見。
3 )在時(shí)間軸第一幀上輸入下面代碼。
4 程序
4.1 字符隨機(jī)出現(xiàn)在屏幕上
var arr:Array=new Array();
var time:uint=0;//字母出現(xiàn)的時(shí)間間隔
var angle:Number=0;//旋轉(zhuǎn)的角度
var score:int=1;
var t:int=13;
var flag:int=1;//定義一標(biāo)志
[SWF(width=800,height=465,backgroundColor=0xffffff,frameRate=24)]//定義環(huán)境
stage.addEventListener(Event.ENTER_FRAME,showChar);//通過(guò)幀頻事件顯示字符
function showChar(e:Event):void{//顯示字符
time++;//延時(shí)
angle+=0.1;
if(time>=t){//字符出現(xiàn)的時(shí)間間隔
var tf:TextField=addChild(new TextField()) as TextField;//建立文本框
time=0;//重新調(diào)用showChar時(shí),保證time初值為0
//在文本框中隨機(jī)生成字符,并設(shè)置其格式
//A
tf.htmlText=''+
String.fromCharCode(int(Math.random()*26+65).toString())+'
';
tf.x=Math.random()*-760+780;//將字符隨機(jī)定位
tf.y=-10;
tf.name=(Math.random()*-10+5).toString();//產(chǎn)生-5~5之間的數(shù)字成為tf的名字
arr.unshift(tf);//將元素添加到數(shù)組的開頭
}
for(var i:Number=arr.length-1;i>=0;i--){//在屏幕上移動(dòng)字符,越界后消失
arr[i].y+=3;//字符往下移
arr[i].x+=Math.sin(angle)*int(arr[i].name);//在X軸上左右擺動(dòng)
if(arr[i].y>=stage.stageHeight){
removeChild(arr[i]);//清除舞臺(tái)上的該字符
arr.splice(i,1);//從數(shù)組中刪除
score--;
break;
}
}
lbl1.text="你的積分是:"+score;
if(score==0){
stage.removeEventListener(Event.ENTER_FRAME,showChar);
timer.stop();
msg.text="挑戰(zhàn)失敗,請(qǐng)退出系統(tǒng)!";
btn.visible=true;
btn.label="退出";
btn.addEventListener(MouseEvent.CLICK,quit);
function quit(e:Event):void{
fscommand("quit");//退出系統(tǒng)
}
}
}
4.2 鍵盤敲擊字母
stage.addEventListener(KeyboardEvent.KEY_DOWN,hitChar);//監(jiān)聽鍵盤事件
function hitChar(e:KeyboardEvent):void{
for(var i:Number=arr.length-1;i>=0;i--){//遍歷數(shù)組
//如果第i個(gè)文本框中的字符等于按鍵的字符
if(arr[i].text==String.fromCharCode(e.keyCode)){
removeChild(arr[i]);
arr.splice(i,1);
score++;
break;
}
}
}
4.3 倒計(jì)時(shí)
var i:int=30;
var timer:Timer=new Timer(1000,30);//每隔1000毫秒調(diào)用timerHandle()一次,共調(diào)用30次
timer.addEventListener(TimerEvent.TIMER,timerHandle);
timer.start();
function timerHandle(e:TimerEvent):void{
i--;//i為時(shí)間,初始為30
lbl.text="倒計(jì)時(shí):"+i.toString();
if(i==0){
timer.stop();
stage.removeEventListener(Event.ENTER_FRAME,showChar);
msg.text="你挑戰(zhàn)失敗,請(qǐng)退出系統(tǒng)!";
btn.visible=true;
btn.label="退出";
btn.addEventListener(MouseEvent.CLICK,quit);
function quit(e:Event):void{
fscommand("quit");//退出系統(tǒng)
}
}
4.4 過(guò)第一關(guān)
if(score>15&&i>0&&flag==1){//在給定時(shí)間積分大于15,過(guò)第一關(guān)
stopListener();
}
4.5 過(guò)第二關(guān)
if(score>=20&&i>0&&flag==2){//過(guò)第二關(guān),flag=2表明是經(jīng)過(guò)了第一關(guān)
stopListener();
}
}
4.6 過(guò)第三關(guān)
if(score>=25&&i>0&&flag==3){
msg.visible=true;
msg.text="挑戰(zhàn)成功,恭喜你獲得:\n由娃哈哈提供的礦泉水一瓶!";
btn.visible=true;
btn.label="退出";
timer.stop();
stage.removeEventListener(Event.ENTER_FRAME,showChar);
btn.addEventListener(MouseEvent.CLICK,nextOK3);
function nextOK3(e:MouseEvent):void{
fscommand("quit");
}
}
4.7 過(guò)關(guān)和終止函數(shù)
function passGate():void{
i=30;//計(jì)時(shí)的初始值
score=1;//計(jì)分
btn.visible=false;//隱藏按鈕
msg.visible=false;//隱藏文本框
timer.start();//重新啟動(dòng)定時(shí)器
timer.addEventListener(Event.ENTER_FRAME,timerHandle);//重新計(jì)時(shí)
stage.addEventListener(Event.ENTER_FRAME,showChar);
}
function stopListener():void{//失敗終止事件
msg.visible=true;
msg.text="恭喜你,成功過(guò)關(guān)!";
btn.visible=true;
btn.label="第”+flag.toString()+”關(guān)";
flag++;
timer.stop();
stage.removeEventListener(Event.ENTER_FRAME,showChar);
btn.addEventListener(MouseEvent.CLICK,nextOK);
function nextOK(e:MouseEvent):void{//進(jìn)入第二關(guān)
if(flag==2){
t=8;//字符出現(xiàn)的時(shí)間間隔減少
passGate();
}else if(flag==3){
t=3;//時(shí)間間隔越小,字母出現(xiàn)越快
passGate();
}
}
}
5 結(jié)束語(yǔ)
隨著網(wǎng)絡(luò)的的發(fā)展,原來(lái)的網(wǎng)頁(yè)端體驗(yàn)已經(jīng)不能滿足用戶需求,富客戶端技術(shù)顯得日益重要,而flashActionScript3.0(簡(jiǎn)稱AS3)為富客戶端技術(shù)提供了重要的解決方案。
參考文獻(xiàn):
[1]夏敏捷.Flash ActionScript 3.0動(dòng)畫基礎(chǔ)與游戲設(shè)計(jì)[M]. 北京: 清華大學(xué)出版社, 2015.
[2] 安永梅, 成維麗.Flash CS5動(dòng)畫制作與應(yīng)用[M]. 2版. 北京: 人民郵電出版社, 2013.