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灯, 水草

请输入看板名称,例如:BuyTogether站内搜寻

TOP