Flash 板


LINE

/* This code is from "ActionScript 3.0 Game Programming University" by Gary Rosenzweig Copyright 2007 http://flashgameu.com See the book or site for more information */ package { import flash.display.*; import flash.events.*; import flash.text.*; import flash.utils.Timer; import flash.media.Sound; import flash.media.SoundChannel; import flash.net.URLRequest; public class MemoryGame extends Sprite { static const numLights:uint = 5; private var lights:Array; // list of light objects private var playOrder:Array; // growing sequence private var repeatOrder:Array; // text message private var textMessage:TextField; private var textScore:TextField; // timers private var lightTimer:Timer; private var offTimer:Timer; var gameMode:String; // play or replay var currentSelection:MovieClip = null; var soundList:Array = new Array(); // hold sounds public function MemoryGame() { // text formating var textFormat = new TextFormat(); textFormat.font = "Arial"; textFormat.size = 24; textFormat.align = "center"; // create the upper text field textMessage = new TextField(); textMessage.width = 550; textMessage.y = 110; textMessage.selectable = false; textMessage.defaultTextFormat = textFormat; addChild(textMessage); // create the lower text field textScore = new TextField(); textScore.width = 550; textScore.y = 250; textMessage.selectable = false; textScore.defaultTextFormat = textFormat; addChild(textScore); // load the sounds soundList = new Array(); for(var i:uint=1;i<=5;i++) { var thisSound:Sound = new Sound(); var req:URLRequest = new URLRequest("note"+i+".mp3"); thisSound.load(req); soundList.push(thisSound); } // make lights lights = new Array(); for(i=0;i<numLights;i++) { var thisLight:Light = new Light(); thisLight.lightColors.gotoAndStop(i+1); // show proper frame thisLight.x = i*75+100; // position thisLight.y = 175; thisLight.lightNum = i; // remember light number lights.push(thisLight); // add to array of lights addChild(thisLight); // add to screen thisLight.addEventListener(MouseEvent.CLICK,clickLight); // listen for clicks thisLight.buttonMode = true; } // reset sequence, do first turn playOrder = new Array(); gameMode = "play"; nextTurn(); } // add one to the sequence and start public function nextTurn() { // add new light to sequence var r:uint = Math.floor(Math.random()*numLights); playOrder.push(r); // show text textMessage.text = "Watch and Listen."; textScore.text = "Sequence Length: "+playOrder.length; // set up timers to show sequence lightTimer = new Timer(1000,playOrder.length+1); lightTimer.addEventListener(TimerEvent.TIMER,lightSequence); // start timer lightTimer.start(); } // play next in sequence public function lightSequence(event:TimerEvent) { // where are we in the sequence var playStep:uint = event.currentTarget.currentCount-1; if (playStep < playOrder.length) { // not last time lightOn(playOrder[playStep]); } else { // sequence over startPlayerRepeat(); } } // start player repetion public function startPlayerRepeat() { currentSelection = null; textMessage.text = "Repeat."; gameMode = "replay"; repeatOrder = playOrder.concat(); } // turn on light and set timer to turn it off public function lightOn(newLight) { soundList[newLight].play(); // play sound currentSelection = lights[newLight]; currentSelection.gotoAndStop(2); // turn on light offTimer = new Timer(500,1); // remember to turn it off offTimer.addEventListener(TimerEvent.TIMER_COMPLETE,lightOff); //如果將 Timer 實體設定為固定次數的間隔,那麼當達到最後的間隔時,也會傳送 timerComplete 事件 offTimer.start(); } // turn off light if it is still on public function lightOff(event:TimerEvent) { if (currentSelection != null) { currentSelection.gotoAndStop(1);//將播放磁頭移到場景中的指定影格,並停止播放 currentSelection = null; offTimer.stop(); } } // receive mouse clicks on lights public function clickLight(event:MouseEvent) { // prevent mouse clicks while showing sequence if (gameMode != "replay") return; // turn off light if it hasn't gone off by itself lightOff(null);//NULL 取代所排除的選用參數 // correct match if (event.currentTarget.lightNum == repeatOrder.shift()) { lightOn(event.currentTarget.lightNum); //event.currentTarget屬性會儲存目前正在處理事件物件之顯示清單物件的參考// // check to see if sequence is over if (repeatOrder.length == 0) { //每個字串都有 length 屬性,其值等於字串中的字元數目 nextTurn(); } // got it wrong } else { textMessage.text = "Game Over!"; gameMode = "gameover"; } } } } --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.120.73.207
1F:→ pm2001:大家好,這是一個as3.0的語法,報告到此結束,謝謝 01/08 01:12
2F:推 etrexetrex:沒事出什麼報告別人code的題目.... 01/08 01:39
3F:→ scars:..................................... 01/08 01:57
4F:→ xup654vu06:別這樣!!因為很急!所以才會丟上來!不知道有沒有強者幫쀠 01/08 02:01
5F:→ xup654vu06:幫我麻煩各位強者 01/08 02:02
6F:推 aquarianboy:第5行:See the book or site for more information 01/08 09:42
7F:推 cjcat2266:先compile出來看看結果,然後再推導吧?感覺很花時間... 01/08 11:03
8F:推 Jedic:這老師八成也是隨便出的,因為他不知道用什麼來打成績 01/08 14:58
9F:→ Jedic:只好隨便抓一個好像很有水準的題目,但是他自己懂嗎? 01/08 14:59







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Boy-Girl站內搜尋

TOP